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

How to fix bug where script runs in studio but not in real game or local server test?

Asked by 6 years ago
local debounce = false

game.Workspace.Throne.Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        if not debounce then
            debounce = true
            while hit.Parent.Humanoid.Health > 0 do
                game.Players.LocalPlayer:WaitForChild('leaderstats').Money.Value = game.Players.LocalPlayer:WaitForChild('leaderstats').Money.Value + 10
                wait(5)
            end
            wait(2)
            debounce = false
        end
    end
end)

When I test this in studio it works perfectly, but If i use a Local server test or if I go into the real game this script does not work at all.

0
is this in a "Local Script" or "Script"? Paldi 109 — 6y
0
He should be since it works in studio, and since he is using localplayer. AbandonedRick 112 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Hi there,

Your game seems to be filtering enabled, and thus does not work in the actual game. As local scripts act like server scripts in studio, they will not turn out an error. However, in game, local scripts act like local scripts (local scripts can't access Server services such as what you are trying to access:Workspace) and only run on the client. And thankfully, your script does not require that you use a local script. However in the future, if you'd like to know more about server script to client script communication, you may want to read this: http://wiki.roblox.com/index.php?title=Remote_Events_and_Functions

So, in your script, you will not have to get the local player but instead get the touched part's parent which is the player who touches the part. Instead of getting the player, you can find for the the player's name in game.Players or use GetPlayerFromCharacter(). Here's how your script should look like.

In a normal script:

local debounce = false

game.Workspace.Throne.Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        if not debounce then
            debounce = true
            while hit.Parent.Humanoid.Health > 0 do
        --Get Player
        local player = hit.Parent.GetPlayerFromCharacter()
                player:WaitForChild('leaderstats').Money.Value = player.leaderstats.Money.Value + 10
                wait(5)
            end
            wait(2)
            debounce = false
        end
    end
end)

Note:GetPlayerFromCharacter() may sometimes not work if so, try this script instead:

local debounce = false

game.Workspace.Throne.Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        if not debounce then
            debounce = true
            while hit.Parent.Humanoid.Health > 0 do
        --Find for player in game.Players based on character names which conveniently is the same as the player's name
        local player = game.Players:FindFirstChild(hit.Parent.Name)
                player:WaitForChild('leaderstats').Money.Value = player.leaderstats.Money.Value + 10
                wait(5)
            end
            wait(2)
            debounce = false
        end
    end
end)
0
Thank you so much it works perfectly now! FirestormFury 7 — 6y
0
No problem! AbandonedRick 112 — 6y
Ad

Answer this question