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 Play Solo, but not Start Server / Published to Roblox?

Asked by
Xduel 211 Moderation Voter
10 years ago

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

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

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!

Ad

Answer this question