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

Why does my script only work in roblox studio and not in the actual game?

Asked by 6 years ago

I am making a tycoon, and the part that does not work is when the item touches the furnace, it gives you money. It works perfectly in studio but not in game. Can someone explain why? Thanks.

here is the script:

local Part = script.Parent

Part.Touched:connect(function(hit)
    if hit.Name == "Shirt" then
        wait(1)
        hit:Destroy()
        game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value + 20
    end
end)
0
is this in a localscript or server script? ax_gold 360 — 6y
0
This won't work in either, because you use LocalPlayer, but this script is in the workspace, ServerScripts can't use LocalPlayer, and LocalScripts can't be in the workspace theCJarmy7 1293 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Your script doesn't work online because the script is running on the server which is also the client in Studio. To make it work for online games, change your code to the one below.

local Part = script.Parent

Part.Touched:connect(function(hit)
    if hit.Parent then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent);
        if player then
              player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 20
        end
    end
    end
end)

Let me tell you how the script works.

  1. It listens for a Touched event. If the event fires, it checks hit's Parent (the character) and then tries to get the player owning it.

  2. If it returns the player, use that player and change the amount of cash to your desired value. If nil, just ignore the changing sequence and go back to step 1.

0
Awesome thank you :) xualex10 3 — 6y
0
No problemo! xXprohax0r1337Xx 74 — 6y
Ad

Answer this question