Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My leaderstats checking system sometimes work sometimes does not ?

i am making a script that ensures that player leaderstats value do not go beyond a certain point and if it does it resets the value this is my script it works sometimes and sometimes don't

game.Players.PlayerAdded:Connect(function(player)
	local function checks()
		if player.leaderstats.Weight.Value > player.Capacity.Value then
			player.leaderstats.Weight.Value = player.Capacity.Value
		end
	end
	
	   while true do 
		checks()   
		wait(0.1)
		checks()
		wait(0.1)
	   end	
	
end)

sometimes i even get a error saying leaderstats is not a valid member of player please help

1 comment

0

you should add a WaitForChild to see if it helps, add this into the playeradded checks function: local leaderstatwait = player:WaitForChild("leaderstats")

1 answer

The reason it is sometimes saying leaderstats is not a valid member of player is because sometimes it has not been created yet so you will need to use WaitForChild to wait for it to be created. You will also have to do that with capacity.

game.Players.PlayerAdded:Connect(function(player)
	local stats = player:WaitForChild("leaderstats")
	local capacity = player:WaitForChild("Capacity")

    local function checks()
        if stats.Weight.Value > capacity.Value then
            stats.Weight.Value = capacity.Value
        end
    end

   while true do 
    checks()   
    wait(0.1)
    checks()
    wait(0.1)
   end  
end)

I also recommend that instead of using a while loop you should used the Changed event which only fires when the value of Weight is changed instead of all the time.

game.Players.PlayerAdded:Connect(function(player)
	local capacity = player:WaitForChild("Capacity")
	local stats = player:WaitForChild("leaderstats")
	
	stats.Weight.Changed:Connect(function(newWeight)
		if newWeight > capacity.Value then
			stats.Weight.Value = capacity.Value
		end
	end)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.