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

Help w/ function not running in a while loop?

So I made this function which works but it does not work in the while loop which is in the if then statement. I checked in the output if "1" was printed and it was, but the function did not run.

function StartJumping()
	game.Players.PlayerAdded:connect(function(Players)
		Players.CharacterAdded:connect(function(Char)
			while true do
				Char:FindFirstChild("Humanoid").Jump = true
				Char:FindFirstChild("Humanoid").Sit = true
				wait(.1)
			end
		end)
	end)
end


Time = 20
while true do
	Time = Time - 1
	print(Time)
	wait(1)
	if Time == 1 then
		StartJumping() --HELP HERE
	end
end

2 comments

0

You're running PlayerAdded after a Player is already added, so that Player won't be affected.

0

How do I fix this problem then?

1 answer

✓ Accepted answer

Once problem is you're running PlayerAdded after a Player is already added, so that Player won't be affected(in the case you're testing it in studio). Also your script will only fire once(the loop) since you're not resetting it, it'll go below 0(-1, -2, etc).

Here's a example of what I did to make it jump all players(I explained the code I added):

function StartJumping(Player) -- Added a variable here
	local Player = game.Players:FindFirstChild(Player.Name) -- Look for the player
	if Player then -- In case it doesn't find it
		repeat wait() until Player.Character -- Wait til it finds the character
		while wait(.1) do
        	Player.Character:FindFirstChild("Humanoid").Jump = true
            Player.Character:FindFirstChild("Humanoid").Sit = true
		end
	end -- End the if function
end


Time = 20
while true do
    Time = Time - 1
    print(Time)
    wait(1)
    if Time == 1 then
		for _,v in pairs(game.Players:GetPlayers()) do -- Gather all the players
			StartJumping(v) -- Make all of them jump
 		end -- End the for _,v in pairs
    end
end

2 comments

0

Thanks, but is it possible for it to work if you did not change up almost everything?

0

@PreyStar Not sure, I don't think so.

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