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

Why doesn't this function work more than one time?

Asked by 7 years ago

works perfectly only the first time it happens, what's the problem?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("FishEvent")
local playerGui = player:WaitForChild("PlayerGui")


local function onWelcomePlayerFired()
do
local henlo = script.Parent.ActivateFish:Clone()
henlo.Parent = script.Parent
henlo.Name = "Clone"
henlo.Disabled = false
wait(4)
            end 
        end 

welcomePlayerEvent.OnClientEvent:Connect(onWelcomePlayerFired)
0
Where is the script located? Also that 'do' is pretty useless. It just makes the code more complicated. TheDeadlyPanther 2460 — 7y
0
The 'wait(4)' is also useless, since it's at the end of a function/coroutine. The problem is likely that the first ActivateFish changes something important so that running it a second time doesn't work. If you printed out every time onWelcomePlayerFired, I expect you'd find that the function is running every time the event is fired. chess123mate 5873 — 7y

1 answer

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

First let's get your variables down

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("FishEvent")
local playerGui = player:WaitForChild("PlayerGui")

The do on line 10 is pretty much useless so let's get rid of it, also lets combine the function and calling it.

welcomePlayerEvent.OnClientEvent:connect(function()

end)

Next i'm going to add your code in with just a bit of rearranging(idk i like it in a order ;p)

welcomePlayerEvent.OnClientEvent:connect(function()
    local henlo = script.Parent.ActivateFish:Clone()
    henlo.Name = "Clone"
    henlo.Parent = script.Parent
    henlo.Disabled = false
end)

No point in adding the wait(4) because the event can be called over and over

Let's put it together

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local welcomePlayerEvent = ReplicatedStorage:WaitForChild("FishEvent")
local playerGui = player:WaitForChild("PlayerGui")

welcomePlayerEvent.OnClientEvent:connect(function()
    local henlo = script.Parent.ActivateFish:Clone()
    henlo.Name = "Clone"
    henlo.Parent = script.Parent
    henlo.Disabled = false
end)

As long as no other script is deleting that ActivateFish instance you should be fine.

0
Thanks but this doesn't change the fact that it only works one time greybird700 63 — 7y
0
@greybird700 if the event is only being called one time then that means YOU are only calling it one time, this part of the script is correct, maybe your problem lies in the script you are firing the event from. DanzLua 2879 — 7y
Ad

Answer this question