Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
0
Answered by 7 years ago

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
0
This one doesn't work at all and the error is with the else greybird700 63 — 7y
0
once the else is removed there are no errors but it still doesn't work greybird700 63 — 7y
0
Because this script is bad. OldPalHappy 1477 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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)

Answer this question