Leaderstats script only work when playing inside the studio

For some weird reason my script only work when playing inside the studio and did not work on local server and public server and i tried to fix it but i cant still get it to work, im still a beginner scripter


this is from the studio its working properly


this is from the local server and it did not work

What im trying to make here is a distance script with a maximum distance from the center of map

This is the script that i placed on serverscriptserveice:

function onPlayerEntered(newPlayer)
	wait(game.Loaded)

	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local dist = Instance.new("IntValue")
	dist.Name = "Distance"
	dist.Value = 0
	dist.Parent = stats

	local max = Instance.new("IntValue")
	max.Name = "Max"
	max.Value = 0
	max.Parent = stats
	
	stats.Parent = newPlayer

	while true do
		wait(0.5)
		dist.Value = math.round(newPlayer.Character.HumanoidRootPart.CFrame.Position.Z)
		if dist.Value >= max.Value then
			max.Value = dist.Value
		end
	end
end

game.Players.ChildAdded:connect(onPlayerEntered)

anyone know what is wrong with this script?

can you tell me what’s the purpose of that?

Also use Players.PlayerAdded
and if issue persists try wrapping the loop in a spawn function or coroutine

task.spawn(function()
while true do
		wait(0.5)
		dist.Value = math.round(newPlayer.Character.HumanoidRootPart.CFrame.Position.Z)
		if dist.Value >= max.Value then
			max.Value = dist.Value
		end
	end
end)
1 Like

im surprised your script runs at all.
first of wait(game.Loaded) will not work because wait() expects a number and .Loaded is a event.
im pretty sure .Loaded is also client only.

your onPlayerEntered function expects a argument named newPlayer. you should do:

game.Players.ChildAdded:Connect(function(player)
    onPlayerEntered(player)
end)
2 Likes

i tried using that but that dosent worked
here is the current script

function onPlayerEntered(newPlayer)

	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local dist = Instance.new("IntValue")
	dist.Name = "Distance"
	dist.Value = 0
	dist.Parent = stats

	local max = Instance.new("IntValue")
	max.Name = "Max"
	max.Value = 0
	max.Parent = stats
	
	stats.Parent = newPlayer

	while true do
		wait(0.5)
		dist.Value = math.round(newPlayer.Character.HumanoidRootPart.CFrame.Position.Z)
		if dist.Value >= max.Value then
			max.Value = dist.Value
		end
	end
end

game.Players.ChildAdded:Connect(function(player)
	onPlayerEntered(player)
end)

This is from the log vvv

this is because the players character hasnt been created yet, you should try doing newPlayer.CharacterAdded:Wait() after you parent the stats

1 Like

Thanks, its working now. i was using wait(game.loaded) because i thought the script dosent work because the game was not loaded

As the other person said, even that wouldn’t work since loaded is an event, not a number, if you wanted to do that you would do game.Loaded:Wait()

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