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

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

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.

2 comments

0

is this in a "Local Script" or "Script"?

0

He should be since it works in studio, and since he is using localplayer.

1 answer

✓ Accepted answer

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)

2 comments

0

Thank you so much it works perfectly now!

0

No problem!

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