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:

1function cloneLocalScript(playerWhoTouched)
2local userCharacter = playerWhoTouched.Parent
3local userPlayer = game.Players:GetPlayerFromCharacter(userCharacter)
4script.LocalScript:clone().Parent = userPlayer.PlayerGui.cameraStuff
5end
6 
7script.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.

01local touched = false
02function cloneLocalScript(playerWhoTouched)
03    if not touched then
04    touched = true
05    local userCharacter = playerWhoTouched.Parent
06    local userPlayer = game.Players:GetPlayerFromCharacter(userCharacter)
07        if not userPlayer.PlayerGui.cameraStuff:findFirstChild('LocalScript') then
08        script.LocalScript:clone().Parent = userPlayer.PlayerGui.cameraStuff
09        end
10    touched = false
11    end
12end
13 
14script.Parent.Touched:connect(cloneLocalScript)
0
Thanks TSK_Uncovered 55 — 8y
Ad

Answer this question