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

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

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.

3 comments

0
0

Will this work if several players were to touch it?

0

it would only allow the code to be ran once than after its been executed then it allowes another request

1 answer

✓ Accepted answer

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)

1 comment

0

Thanks

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