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

Items are cloning multiple times instead of just once. How would I go about Limiting this?

I am cloning accessories to the player character using a remote event. Items are placed within Replicated Storage. My goal is to not allow multiple clones of the accessory The remote event is fired using a Button Gui. Clicking the button multiple times will result in multiple clones

The Script

local rs = game:GetService("ReplicatedStorage")
local af = rs:WaitForChild("Accessories")
local remotesFolder = af:WaitForChild("Remotes")
local RemoteEvent = remotesFolder.EquipHat --Remote Function






	
	
	RemoteEvent.OnServerEvent:Connect(function(player, topic, info) 
	if topic == "Equip Accessory" then
		local newAccessory = info:Clone() --Clone
		newAccessory.Parent = player.Character --Places in player character
	end
	end)

I would very much prefer one click used to clone accessory and a second click deleting the clone But I am not advanced enough to understand how to go about doing so.

Any help is much appreciated.

2 answers

✓ Accepted answer

This could work?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Accessories = ReplicatedStorage:WaitForChild("Accessories")
local remotesFolder = Accessories:WaitForChild("Remotes")
local RemoteEvent = remotesFolder.EquipHat



RemoteEvent.OnServerEvent:Connect(function(player, topic, info) 
    if topic == "Equip Accessory" and not player.Character:FindFirstChild(info) then
        local newAccessory = info:Clone()
        newAccessory.Parent = player.Character
    elseif topic == "Equip Accessory" and player.Character:FindFirstChild(info) then
        player.Character[info]:Destroy() 
    end
end)

I'm not sure if this is the same as the other guys but I would make a variable defining whether the accessory is on or off. (you would adjust this to your script) here it is:

accessorycheck = false --setting the variable to false

if accessorycheck == false then --checking if the variable is false
buttontype = 1 --changes the buttons type (could change the image and/or your code)
else
buttontype = 2
end

--your code would probably go here and this is where you would be checking if the button is pushed. Just make sure you change the variable when you do so.

This probably won't work but give it a try if you can :)

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