Code only works twice then stops

Basically I’m trying to make it so when the player types /e invert their game gets inverted. It works and you can fix the invert but then you can’t use the invert again. (Not Intentional)

How do I make it so it can be used again?

task.wait()

InvEvent = game.ReplicatedStorage.Events.Invert

Con = InvEvent.OnClientEvent:Connect(function()
	
	if workspace:FindFirstChild("InvertHighlight") == nil then
		script.Parent:Clone().Parent = workspace
		Con:Disconnect()
	else
		workspace:FindFirstChild("InvertHighlight"):Destroy()
		--Con:Disconnect()
	end	
	
end)

You will need to reconnect the connection after disconnecting it, or else it will not run the function again. Is there any reason why you need to disconnect the connection? It seems like it would work fine without it.

when i dont disconnect it, the code doesnt run the else line.

When you do this, it clones the script as well. This is likely the cause of your issue. Try deleting or disabling the script before you parent the clone to the workspace.

Hey,

Bob may be right here. Perhaps have a bool and state not inverted each time the event is triggered. It is a lot less memory heavy and a lot simpler to understand.

Thanks

Thank you! It ended up working.
New Code

task.wait()

InvEvent = game.ReplicatedStorage.Events.Invert

Con = InvEvent.OnClientEvent:Connect(function()
	
	if workspace:FindFirstChild("InvertHighlight") == nil then
		local hlight = script.Parent:Clone()
		
		hlight.Invert:Destroy()
		hlight.Parent = workspace

	else
		
		workspace:FindFirstChild("InvertHighlight"):Destroy()

	end	
	
end)

I was doing that before but I just switched to relying on finding the highlight inside the workspace.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.