Ok so, this is my second question here and my second day here so please excuse any mistakes in this question, im also new to lua and trying to self learn, but recently i've been going crazy trying to self-learn how to fix these errors that keep happening to me, but I gave up trying to fix them myself, so I came here.
So I have a rock in my game, with a click detector and a normal server script in it, with the following code:
script.Parent.ClickDetector.MouseClick:connect(function(Player)
local CollectREMOTE = game:GetService("ReplicatedStorage").Remotes.Collect2
CollectREMOTE:FireClient(Player, "Rock", script.Parent.Name) --Script.parent.name basically is used so the server can make sure the client isn't lying, it checks to make sure it exists before rewarding them.
end)
I debuged via printing and the script above, doesnt work after the first click. Once I click it, the code above works ONCE, and everything works smoothly. Once the code finishes running, I click it again, nada, this code doesnt work/run the second time.
If you want to know where that fireclient leads, it leads to the client handler which fires the server, which the server then checks its name, if it exists, if its nil, and if its already collected, and other stuff.
Then the server fires the client to update the client, and the client executes the following code:
game:GetService("ReplicatedStorage").Remotes.UpdateClient.OnClientEvent:Connect(function(itemName)
print("Remote fired")
local OldParent = game.Workspace.Collectables
local NewParent = game.Workspace.Collected
print("Defined parents")
game.Workspace.Collectables[itemName].Parent = NewParent
print("Switched parents")
NewParent[itemName].Transparency = 1
print("Changed trans")
NewParent[itemName].ClickDetector:Destroy()
print"destroyed"
wait(2)
print"waited 2"
NewParent[itemName].Parent = OldParent
print"switched again"
OldParent[itemName].Transparency = 0
print"reset trans"
local NewClickDetector = Instance.new("ClickDetector")
print"made new click"
NewClickDetector.Parent = OldParent[itemName]
print"parented it"
NewClickDetector.Name = "ClickDetector"
print"named it"
print("Done!")
end)
Ignore the prints, I used it for debugging.
I think it has something to do with me destroying ClickDetector, I dont really know. I did it because if you made it transparent it would still be clickable, and if the player clicked it while it was not respawned yet the server would kick the client for "exploiting". But I made a instance.new("clickdetector") so I dont know why that wouldnt work. I dont know if me destroying clickdetector is the issue though, but thats my guess.
Can someone please help me with this? Thank you!

2 comments
It definitely has something to do with you destroying the ClickDetector. When you destroy a ClickDetector in a LocalScript and create a new one, the server does not see that new one as the one that you destroyed(on the client side).
instead of destroying the clickdetector, just parent the click detector to game.serverstorage then when you need it again parent it back