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

Why does my script work In Play Solo, but not Start Server / Published to Roblox?

So in this script, I have a part that is stepped on. If the player steps on that part and presses the key "r", a value known as Int will become true.

script.Parent.Touched:connect(function(player)
	plr = game.Players:GetPlayerFromCharacter(player.Parent)
	mouse = plr:GetMouse()
		mouse.KeyDown:connect(function(key)
			if key == "r" then
			game.Workspace.Int.Value = true
		end
	end)
end)

For some reason, however, this script will only work in Play Solo mode, not Start Server or Published to Roblox. What did I do wrong? Is it a LUA glitch or my own error? As someone who doesn't know to much on LUA's technical side, I don't know what's wrong here. All answers appreciated, thank you. ~ Xduel

1 answer

You made a mistake on the Touched. Not everything that touches it would be a player. Also, I added a debounce for you so multiple players can hit it.

function debounce(func)
    local isRunning = false  
    return function(...)      
        if not isRunning then
            isRunning = true
 
            func(...)   
 
            isRunning = false
        end
    end
end

script.Parent.Touched:connect(debounce(function(player)
	if player.Parent and game.Players:GetPlayerFromCharacter(player.Parent) then
    plr = game.Players:GetPlayerFromCharacter(player.Parent)
    mouse = plr:GetMouse()
        mouse.KeyDown:connect(function(key)
            if key:lower() == "r" then
            game.Workspace.Int.Value = true
end
end)
	end
end))

~Thank me by accepting this answer/bumping up my reputation!

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