This is my code, if your wondering, but I still can’t figure it out.
Oh you didn’t add a debounce so it would keep running through if the item is equipped. So to fix this do
local debounce = false
AxeEvent.OnServerEvent:Connect(function(Player,Frame)
isEquipped = not isEquipped
if isEquiooed and not Deboucne then
Debounce = true
--Code
wait(1)
Deboucne = false
--Code
Okay if not this I’m at a bust sorry. Try removing the wait(1) and debounce = false because it’s already setting at false at the beginning.
Couple things I think should be changed:
Declare your Debounce variable outside of the OnServerEvent:Connect function. Currently, it is declared within the function so it will always be false every time the event happens, so it currently has no effect.
The isEquipped logic looks backwards. It should be more like:
if not isEquipped and not Debounce then
-- Equip tool
elseif isEquipped ...
-- Unequip tool
end
Another possibility is that it’s trying to compare the object addresses for the Color3 objects, versus comparing RBG values of the Color3 objects. To fix this, you could either get the values of the Frame’s BackgroundColor3 and compare them each separately, or you may be able to declare a Color3 object outside of the function, and use that Color3 object to assign and compare Frame.BackgroundColor3 within the function. The second option would be better, if it works as intended. Actually, you probably don’t even need to check the color of the Frame.BackgroundColor3 in the elseif-check and it should still behave the same.
I’m not sure if those things alone will fix the problem or if there’s more issues. It’s always more difficult to analyze conditional statements when each test has multiple conditions, e.g. if A and B, elseif X and Y and Z… it won’t hurt performance to nest if-statements once in a while.
Btw, it would be easier for people to help offer you code modifications if you paste your code as text instead of screenshots. That way, people can copy and paste and edit your code instead of typing it all from scratch.
I have started using a different way, but I’m now having problems destroying them, and i’m still wondering what the issue is since I just copied the code I wrote for the old way I used, and it wouldn’t work.
local Tool = game:GetService("ReplicatedStorage").Tool
Tool.OnServerEvent:Connect(function(Player, Tool, Frame)
isEquipped = not isEquipped
local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(Tool):Clone()
if isEquipped then
UndefinedTool.Parent = Player.Backpack
Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
elseif isEquipped and UndefinedTool then
Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
UndefinedTool:Destroy()
UndefinedTool = nil
end
end)
Try this:
local int = 0
int += 1
if int >1 then
local oldaxe = player.Backpack:FindFirstChild("Axe")
if oldaxe then
oldaxe:Destroy()
end
end
There might be some mismatches going on. It looks like Tool is a RemoteEvent, but it’s also a tool? Plus, if you try to pass an object between client and server, I don’t think the receiver can try to look for the object from its address, which is what I think you’re trying to do.
Also, the isEquipped logic still looks backwards.
Try something like this to see if it works better… I’m not sure if you had a RemoteEvent and a tool with the same name, so I renamed what I think was an event to EquipToolEvent. I also changed where and how isEquipped is assigned, and some other misc changes.
local EquipToolEvent = game:GetService("ReplicatedStorage").EquipToolEvent
EquipToolEvent.OnServerEvent:Connect(function(Player, Tool, Frame)
local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(Tool.Name):Clone()
if not isEquipped then
-- Equip tool
UndefinedTool.Parent = Player.Backpack
Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
isEquipped = true
else
-- Unequip tool
if UndefinedTool then
Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
UndefinedTool:Destroy()
UndefinedTool = nil
isEquipped = false
end
end
end)
It seems like it still duplicates the tool
It just seems like the script isn’t going past the else statement, which is really confusing.
It’s not going into the else statement bc of this line:
local isEquipped = false
Also, I think what I said about the RemoveEvent just added confusion, so try this instead. Note that it’s referring to Tool.Name in the WaitForChild call.
local Tool = game:GetService("ReplicatedStorage").Tool
Tool.OnServerEvent:Connect(function(Player, Tool, Frame)
local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(Tool.Name):Clone()
if not isEquipped then
-- Equip tool
UndefinedTool.Parent = Player.Backpack
Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
isEquipped = true
else
-- Unequip tool
if UndefinedTool then
Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
UndefinedTool:Destroy()
UndefinedTool = nil
isEquipped = false
end
end
end)
Yeah but, i’m sending the name of the tool to the server, as a string.
And also, what is isEquipped supposed to be then? since its not a variable nor defined to anything.
Ah, okay. Probably better to name the arg something like ToolName then to avoid confusion.
Based on your original code, isEquipped seems to be defined somewhere else, which should be fine. It just can’t be defined within the function, because its value has to exist outside of the scope of the function for you to be able to toggle it properly.
Sorry about that, also it is changing the color of the tool to the color requested in the else statement, but it doesn’t destroy the tool, is that a problem?
Oh yeah, what it’s doing now is destroying the clone that was created within this function. In the else-statement, you would do a find in the player’s backpack for the ToolName, then if found you would destroy it.
Here is the updated code. I used ToolName as the arg. I also moved the cloning into the block that only executes if the tool is equipped. I can’t test this right now so there could be a mistake in the new code I added.
local Tool = game:GetService("ReplicatedStorage").Tool
Tool.OnServerEvent:Connect(function(Player, ToolName, Frame)
if not isEquipped then
-- Equip tool
local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(ToolName):Clone()
UndefinedTool.Parent = Player.Backpack
Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
isEquipped = true
else
-- Unequip tool
local TheTool = Player.Backpack:FindFirstChild(ToolName)
if TheTool then
Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
TheTool:Destroy()
isEquipped = false
end
end
end)