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

Why does my script work in studio, but not in an actual server?

This is a decently simple script that completely works in studio. When I actually test it in game, it does't work. Does anyone know why?

script.Parent.Touched:connect(function(part)
	local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
	local plr = game.Players:GetPlayerFromCharacter(part.Parent)
	
	if leaderstats["Raw Robloxs"].Value ~= 0 then
	wait(0.1)
	leaderstats.Money.Value = leaderstats.Money.Value + leaderstats["Raw Robloxs"].Value
	wait(0.1)
	leaderstats["Raw Robloxs"].Value = 0	
	end
	
end)

1 answer

✓ Accepted answer

'LocalPlayer' is for the client only, wich means that the server cannot access it.

A fix would be

script.Parent.Touched:connect(function(part)
    local plr = game.Players:GetPlayerFromCharacter(part.Parent)

   if plr then
	local leaderstats = plr:WaitForChild('leaderstats')
    if leaderstats["Raw Robloxs"].Value ~= 0 then
	    wait(0.1)
	    leaderstats.Money.Value = leaderstats.Money.Value + leaderstats["Raw Robloxs"].Value
	    wait(0.1)
	    leaderstats["Raw Robloxs"].Value = 0    
    end
   end

Btw, your scripts assumes that the 'hit' is a player, never do that, it might be another part, or a random brick.

If you have any more questions (or I made a typo), just ask

2 comments

0

Thanks!

0

Np :)

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