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)
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
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)