Simple script won't work

Hello,

I used to script games a lot, just getting back into it now.
I am making a simple script that changes a value by adding 1 to it every second. For some reason it won’t work at all. Here is the script:


print("test")

repeat wait() until game:IsLoaded()

print("test")

wait(1)

print("test")

game.Players.PlayerAdded:Connect(function(player)

print("test")

while wait(1) do

print("test")

player.leaderstats.Power.Value = player.leaderstats.Power.Value + 1 + player.leaderstats.Rebirths.Value

print("test")

end

print("test")

end)

print("test")

For some reason, it only prints the first “test.”

Is this just a simple and dumb mistake?

Yeah, just simply remove the repeat until loop and the wait(1), thats your issue

1 Like

I tried that, it only goes until before while wait 1.

game:IsLoaded() only works on the client. playerAdded only works on the server. The game is already loaded on the server side, before any scripts run, so remove that.

print("1")
--repeat wait() until game:IsLoaded() --Doesn't work on the server
print("2")
--wait(1) --Player may join before this wait can finish, event wont fire
print("3")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder") --Leaderstats exist yet?
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local power = Instance.new("IntValue")
	power.Name = "Power"
	power.Parent = leaderstats

	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	
	print("4")
	while wait(1) do
		print("5")
		power.Value += 1 + rebirths.Value --Using +- increments
		print("6")
	end
	print("7")
end)

print("8")
1 Like

So you tried this?

print("test")

game.Players.PlayerAdded:Connect(function(player)

print("test")

while wait(1) do

print("test")

player.leaderstats.Power.Value = player.leaderstats.Power.Value + 1 + player.leaderstats.Rebirths.Value

print("test")

end

print("test")

end)

print("test")

Add continue or break inside the loop, that may fix it for you

Also, do the leaderstat values exist yet?
The player may join before the 1 second is up, remove that.

This one is better

print("test")

game.Players.PlayerAdded:Connect(function(player)

print("test")

while wait(1) do

print("test")

player.leaderstats.Power.Value += 1 + player.leaderstats.Rebirths.Value -- Use += it saves characters.

print("test")

end

print("test")

end)

print("test")

@Manby7

That’s literally the same code, the only difference is just the +=, there is no difference in doing that

oh ok⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.