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

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

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)

2 comments

0

Where is the script located? Also that 'do' is pretty useless. It just makes the code more complicated.

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.

1 answer

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.

2 comments

0

Thanks but this doesn't change the fact that it only works one time

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.

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