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

I am unable to figure out how to loop this without crashing. Help?

Hello! I have been trying to get this to loop, and not only go off when you join the game. I tried adding a "while true do" along with "wait(3)" but it caused Roblox studio to crash. Thank you so much.

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(character)
        if game.GamePassService:PlayerHasPass(plr,1131172400) then
            character.Humanoid.WalkSpeed = plr.leaderstats["Steps"].Value + 1
		else character.Humanoid.WalkSpeed = plr.leaderstats["Steps"].Value + 1
        end
    end)
end)

*I need it to update every 3 seconds because the points are going to affect the speed. Thank you for your help.

2 answers

✓ Accepted answer
game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(character)
        while true do
         wait()
        if game.GamePassService:PlayerHasPass(plr,1131172400) then
            character.Humanoid.WalkSpeed = plr.leaderstats["Steps"].Value + 1
        else character.Humanoid.WalkSpeed = plr.leaderstats["Steps"].Value + 1
        end
    end
    end)
end)

Always remember to have a

                                              Wait()

When you use a loop.

Accept answer if helped!

I am assuming that you want the players who have the pass to get benefits in a loop; therefore we can make an array of players with the pass:

local passplayers = {};
local passid = 1131172400;

-- if a player who joins has the pass, add them to our array of players with the pass, "passplayers"
game.Players.PlayerAdded:connect(function(player)
	if(game.GamePassService:PlayerHasPass(player, passid))then
		table.insert(passplayers, player);
	end
end)

-- if a player who leaves has the pass, remove them from the array of players who have the pass
game.Players.PlayerRemoving:connect(function(player)
	for a, player in pairs(passplayers) do
		if(player == player)then
			table.remove(passplayers, a);
		end
	end
end)

-- now, in our main loop, we can do whatever for all players. then, we can iterate over passplayers and perform actions specific to them

while wait(3) do
	-- do whatever normal stuff every 3 seconds
	for a, player in pairs(passplayers) do
		-- do special things for special players
		player.Character.Humanoid.WalkSpeed = player.leaderstats["Steps"].Value + 1;
	end
end

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