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

leaderstats script not working in game but fine in studio?

this script works perfectly in studio, but when i test on a server. Whats wrong with it?

game.Players.PlayerAdded:connect(function(player)
	local leaderstat = Instance.new("Model")
		leaderstat.Parent = player
		leaderstat.Name = "leaderstats"
		
		
	local ROBUX = Instance.new("IntValue")
	ROBUX.Name = "CashDonated"
	ROBUX.Parent = game.Players.LocalPlayer.leaderstats
	ROBUX.Value = 0
		
		
	local PlayTime = Instance.new("IntValue")
	PlayTime.Parent = leaderstat
	PlayTime.Value = 0
	PlayTime.Name = "Play Time"
	local i = 0
	while true do
		i = i + 1
		PlayTime.Value = i
		wait(1)
		
	end
end)

2 comments

0

You can't use LocalPlayer in a Server Script, and leaderstats are kind of need to be run in a Server Script... Line 9, all you need is ROBUX.Parent = leaderstat

0

You can also set a parent without setting it like .Parent by putting it after the instance name with a coma. In this case: Instance.new("IntValue", leaderstat), the leaderstat was already assigned parent to the player, so you could've assigned it to that instead.

2 answers

game.Players.PlayerAdded:connect(function(player)
    local leaderstat = Instance.new("Model")
        leaderstat.Parent = player
        leaderstat.Name = "leaderstats"


    local ROBUX = Instance.new("IntValue")
    ROBUX.Name = "CashDonated"
    ROBUX.Parent = leaderstat
    ROBUX.Value = 0


    local PlayTime = Instance.new("IntValue")
    PlayTime.Parent = leaderstat
    PlayTime.Value = 0
    PlayTime.Name = "Play Time"
    local i = 0
    while true do
        i = i + 1
        PlayTime.Value = i
        wait(1)

    end
end)

You didn't have to refer to LocalPlayer in a server script. Only LocalScript can refer to LocalPlayer. Also, you didn't need to put it like that, the leaderstats was already parented to player so it would've been faster to put it like this:

ROBUX.Parent = leaderstat

Full script:

game.Players.PlayerAdded:connect(function(player)
    local leaderstat = Instance.new("Model")


        leaderstat.Parent = player
        leaderstat.Name = "leaderstats"
   	local ROBUX = Instance.new("IntValue")


    	ROBUX.Name = "CashDonated"
    	ROBUX.Parent = leaderstat
  	ROBUX.Value = 0


    local PlayTime = Instance.new("IntValue")
    PlayTime.Parent = leaderstat
    PlayTime.Value = 0
    PlayTime.Name = "Play Time"
    local i = 0
    while true do
        i = i + 1
        PlayTime.Value = i
        wait(1)
    end
end)

1 comment

0

Dude, i answered first, and my answer works.

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