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

How do make the touched function fire once?

Basically, when someone touches the part it should TP them away and make a GUI appear on their screen. When I go up to it, it clones the GUI numerous times instead of once.

How do I fix this?

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.ClassName == "Model" and hit.Parent.Humanoid ~= nil then
		local character = hit.Parent
		character.HumanoidRootPart.CFrame = CFrame.new(workspace["SecurityTP:A"].Position)
		local player = game.Players:GetPlayerFromCharacter(character)
		local clone = script.ScreenGui:Clone()
		clone.Parent = player.PlayerGui
		clone.Frame.Visible = true
		clone.Frame:TweenSizeAndPosition(UDim2.new(0.466, 0,0.185, 0), UDim2.new(0.266, 0,0.408, 0))
		wait(3)
		clone.Frame:TweenSizeAndPosition(UDim2.new(0,0,0,0), UDim2.new(0.5,0,0.408,0))
		wait(1)
		clone:Destroy()
	end
end)

3 comments

0

I'd say just check the players gui if they have the one and if they dont then add it if they do dont do anything you can alos add in something called a debounce which may help.

0

add a debounce ez pz if debounce = true then ___ end

0

i forgot how to debounce, haven't uses it in a while

1 answer

✓ Accepted answer

You should use a debounce. Its an easy way to detect if a player already touched a part or not.

A debounce is a bool value we make to check if that value is true or not, we can do certain things if its true or false.

You can name it whatever you want.. however its known to be called debounce.. its pretty much like a "cooldown".

debounce = false --It will be false because the player didnt touch the part yet

script.Parent.Touched:Connect(function(hit)
	if debounce == false then debounce = true --It checks if its false, and if it is, it sets it to true.
            if hit.Parent.ClassName == "Model" and hit.Parent.Humanoid ~= nil then
            local character = hit.Parent
            character.HumanoidRootPart.CFrame = 
            CFrame.new(workspace["SecurityTP:A"].Position)
            local player = game.Players:GetPlayerFromCharacter(character)
            local clone = script.ScreenGui:Clone()
            clone.Parent = player.PlayerGui
            clone.Frame.Visible = true
            clone.Frame:TweenSizeAndPosition(UDim2.new(0.466, 0,0.185, 0),UDim2.new(0.266, 0,0.408, 0))
            wait(3)
            clone.Frame:TweenSizeAndPosition(UDim2.new(0,0,0,0),UDim2.new(0.5,0,0.408,0))
            wait(1)
            clone:Destroy()
	    debounce = false --After it has finished, you set it back to false and thats it.
		end
	end
end)

Hope this helped you

:)

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