#weird error on stats thingy

1 messages · Page 1 of 1 (latest)

vapid path
#

`local plr = game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"

local Time = Instance.new("IntValue", leaderstats)
Time.Name = "Time Alive"
Time.Value = 0

local playersInZone = {}

while true do
    local zone = game.Workspace:WaitForChild("SafeZone")
    local playersChecked = {}


    local parts = game.Workspace:GetPartsInPart(zone)

    for i, part in pairs(parts) do
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        if humanoid then
            playersChecked[part.Parent] = true
        end
    end
    
    for character, v in pairs(playersChecked) do
        if not playersInZone[character] then
            Time.Value += 1
            
            
            playersInZone[character] = true
        
        end
    end
    
    for character, v in pairs(playersInZone) do
        if not playersChecked[character] then
            


            playersInZone[character] = nil

        end
    end
end

task.wait(1)

end)`

#

Im trying to make a system that confirms wether the player is inside a zone and add 1 time per second he is outside the zone

#

the error is on line 18

#

for i, part in pairs(parts) do this is line 18

hallow hare
#

ur while loop is running infinitely without yielding

vapid path
#

how can i fix that

hallow hare
#

try moving task.wait(1) inside

#

the while loop

vapid path
#

ohhh

#

bro im so dumb THANKS SO MUCH

#

i really needed help

#

time is adding but very weirdly

faint tulip
#

wdym weirdly

hallow hare
#

so basically

desert schoonerBOT
#

studio** You are now Level 1! **studio

hallow hare
#

you were tracking all players in a script meant for one

hallow hare
#

and incrementing time at the wrong moment

vapid path
hallow hare
#
local plr = game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local Time = Instance.new("IntValue")
    Time.Name = "Time Alive"
    Time.Value = 0
    Time.Parent = leaderstats

    local function isPlayerInZone()
        local character = plr.Character
        if not character then return false end
        local hrp = character:FindFirstChild("HumanoidRootPart")
        local zone = game.Workspace:FindFirstChild("SafeZone")
        if not hrp or not zone then return false end

        -- Check if the player's HumanoidRootPart is within the zone's bounds
        local pos = hrp.Position
        local zPos = zone.Position
        local zSize = zone.Size / 2

        if math.abs(pos.X - zPos.X) <= zSize.X and
            math.abs(pos.Y - zPos.Y) <= zSize.Y and
            math.abs(pos.Z - zPos.Z) <= zSize.Z then
            return true
        end
        return false
    end

    while true do
        if not isPlayerInZone() then
            Time.Value = Time.Value + 1
        end
        task.wait(1)
    end
end)

lazy toe xplain so ill just send the edited script

vapid path
#

thanks bro

faint tulip
#

I still dont understand why people wont just use an extra script for leaderstats

#

or generally for data

vapid path
#

i think its easier to have one script for values

faint tulip
#

you mean playerdata

#

?