It runs through the first time perfectly fine, but if I want to click it a second time I cannot. Been trying to solve it for 10-15 minutes and even took a break from earlier to think about it but can't figure out why.
local blueCrystal = game.Workspace:WaitForChild("Blue_Crystal") local blueClickDetector = blueCrystal.ClickDetector local isBlueSpawned = true local respawnTime = 2 --Incase you don't know it's minutes*60 = respawnTime blueClickDetector.MouseClick:connect(function(player) if isBlueSpawned == true then local playerHumanoid = player.Character.Humanoid playerHumanoid.WalkSpeed = 40 for i = 0, 1, .1 do wait() blueCrystal.Transparency = i end isBlueSpawned = false blueCrystal:Destroy() wait(respawnTime) local newCrystal = game.ReplicatedStorage.Blue_Crystal:Clone() newCrystal.Parent = game.Workspace blueCrystal = newCrystal blueClickDetector = newCrystal.ClickDetector print(blueClickDetector.Parent.Parent) isBlueSpawned = true end end)
Problem
The reason this function works only the first time through is because when the function is called, the new click detector inside of the cloned blue crystal does not have a listener function to respond to the MouseClicked event. Only the first one does.
Solution
Do not define the function as an anonymous one. Put it in a variable and connect the function to the MouseClick event every time the blue crystal is cloned.
local blueCrystal = game.Workspace:WaitForChild("Blue_Crystal") local blueClickDetector = blueCrystal.ClickDetector local isBlueSpawned = true function onClicked(player) if isBlueSpawned == true then local playerHumanoid = player.Character.Humanoid playerHumanoid.WalkSpeed = 40 for i = 0, 1, .1 do wait() blueCrystal.Transparency = i end isBlueSpawned = false blueCrystal:Destroy() wait(respawnTime) local newCrystal = game.ReplicatedStorage.Blue_Crystal:Clone() newCrystal.Parent = game.Workspace blueCrystal = newCrystal blueClickDetector = newCrystal.ClickDetector blueClickDetector.MouseClick:Connect(onClicked) print(blueClickDetector.Parent.Parent) isBlueSpawned = true end end blueClickDetector.MouseClick:Connect(onClicked)