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

Why does this function script only fire once? (may be a simple error)

Basically this script should work more than one time, it works perfect the first time but after the first time it stops working, am I missing something?

local clone = script.Parent.MessageGui:clone()
function onHit(hit)
PlayerName = hit.Parent.Name

clone.Parent = game.Players:FindFirstChild(PlayerName).PlayerGui
wait(0.3)
clone.Tween.Disabled = false
end
script.Parent.Touched:connect(onHit)

2 answers

This answer is assuming that you trying to make touched function.

This is your original, take a look;

local clone = script.Parent.MessageGui:clone()
function onHit(hit)
PlayerName = hit.Parent.Name
 
clone.Parent = game.Players:FindFirstChild(PlayerName).PlayerGui
wait(0.3)
clone.Tween.Disabled = false
end
script.Parent.Touched:connect(onHit)

This is the new script;

local clone = script.Parent.MessageGui:clone()
local part = script.Parent -- let's add a part
local activated = false -- a debounce variable so it won't spam

part.Touched:connect(function(hit) -- when player touched a part
activated = true -- set it to true
PlayerName = hit.Parent.Name
-- functions
clone.Parent = game.Players:FindFirstChild(PlayerName).PlayerGui
wait()
clone.Tween.Disabled = false
wait()
activated = false
else -- anything else
return
end

3 comments

0

This one doesn't work at all and the error is with the else

0

once the else is removed there are no errors but it still doesn't work

0

Because this script is bad.

Here:

  • You're not using local functions

  • You're only cloning the MessageGui once

  • :connect is deprecated

  • You used no tabbing

  • You don't check if what touched the part is a player

  • :clone() is deprecated

  • You're not using local variables

  • You used no debounce

local part = script.Parent
local messageGui = part:WaitForChild("MessageGui")

local debounce = false

local function onHit(hit)
	if not debounce then
		debounce = true

		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	
		if plr then
			local clone = messageGui:Clone()
	
			clone.Parent = plr.PlayerGui
			wait(0.3)
			clone.Tween.Disabled = false
		end
	end

	wait(2)
	debounce = false
end

part.Touched:Connect(onHit)

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