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

How can I make this Touched function not do what it is told to do infinite times?

Asked by 8 years ago

So I have this working script that is going to be able to manipulate the player's camera:

function cloneLocalScript(playerWhoTouched)
local userCharacter = playerWhoTouched.Parent
local userPlayer = game.Players:GetPlayerFromCharacter(userCharacter)
script.LocalScript:clone().Parent = userPlayer.PlayerGui.cameraStuff
end

script.Parent.Touched:connect(cloneLocalScript)

This is the script that will move the local script into the PlayerGui.

The problem is that if the player steps over it once, dozens of scripts will be cloned into the PlayerGui.

Is there a way to stop this from happening; a way to check if there is already one and not allow the player to keep taking?

Thanks in advanced.

0
What you need is a debounce http://wiki.roblox.com/index.php?title=Debounce User#5423 17 — 8y
0
Will this work if several players were to touch it? TSK_Uncovered 55 — 8y
0
it would only allow the code to be ran once than after its been executed then it allowes another request User#5423 17 — 8y

1 answer

Log in to vote
1
Answered by
1N0body 206 Moderation Voter
8 years ago
Edited 8 years ago

Debounce and an if statement.

local touched = false
function cloneLocalScript(playerWhoTouched)
    if not touched then
    touched = true
    local userCharacter = playerWhoTouched.Parent
    local userPlayer = game.Players:GetPlayerFromCharacter(userCharacter)
        if not userPlayer.PlayerGui.cameraStuff:findFirstChild('LocalScript') then
        script.LocalScript:clone().Parent = userPlayer.PlayerGui.cameraStuff
        end
    touched = false
    end
end

script.Parent.Touched:connect(cloneLocalScript)

0
Thanks TSK_Uncovered 55 — 8y
Ad

Answer this question