Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago

'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

0
Thanks! kittonlover101 201 — 6y
0
Np :) User#20388 0 — 6y
Ad

Answer this question