Hello, I am working on a game where a player can buy and equip weapons from a camera-tweening shop. Everything seems to work great, but sometimes it runs the equip and unequip functions out of order, and sometimes it runs both twice so the player ends up with two weapons?? A bit tricky to explain and identify but you can test for yourself here if you want a closer look.
this is most likely where the error happens in the client script, but I am also suspicious something is happening in my updateButton() function, which I will take a closer look at.
client script:
BUY_BUTTON.MouseButton1Click:connect(function()
if not player.ownedtools:FindFirstChild(cameras:FindFirstChild("Camera"..current_camera).Toolname.Value) then
purchase:FireServer(cameras:FindFirstChild("Camera"..current_camera).Cost.Value, cameras:FindFirstChild("Camera"..current_camera).Toolname.Value)
BUY_BUTTON.Visible = false
EQUIP_BUTTON.Visible = true
EQUIP_BUTTON.MouseButton1Click:connect(function() --unequip
if EQUIP_BUTTON.Text ~= "EQUIP" then
game.ReplicatedStorage.toolEquipper:FireServer(cameras:FindFirstChild("Camera"..current_camera).Toolname.Value, false)
EQUIP_BUTTON.BackgroundColor3 = Color3.new(56/255, 202/255, 255/255)
EQUIP_BUTTON.Text = "EQUIP"
equippedTool = nil
print("other thing")
elseif EQUIP_BUTTON.Text ~= "UNEQUIP" then --equip
game.ReplicatedStorage.toolEquipper:FireServer(cameras:FindFirstChild("Camera"..current_camera).Toolname.Value, true)
EQUIP_BUTTON.Text = "UNEQUIP"
EQUIP_BUTTON.BackgroundColor3 = Color3.new(255/255, 0/255, 0/255)
equippedTool = cameras:FindFirstChild("Camera"..current_camera).Toolname.Value
print(equippedTool)
end
end)
end
end)`
I am sorry if this comes up strange, I can never get the preformatted text to work lol.
below is the remote event corresponding with the bit of code above in the serverscript.
equip.OnServerEvent:connect(function(player, tool, equip)
if equip == true then --Equip is true so lets equip the tool
if player.ownedtools:FindFirstChild(tool) then -- if the tool is inside the backpack then continue
--lets unequip the other tools
for i, v in pairs(player.Character:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
print("destroyed a tool in their hands")
wait()
end
end
for i, v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
print("destroyed a tool in their backpack")
wait()
end
end
--this will equip the players tool from their backpack
player.ownedtools:FindFirstChild(tool):Clone().Parent = player.Backpack
end
else --They dont want to equip this tool so lets unequip it
--Unequip the tool
for i, v in pairs(player.Character:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
wait()
end
end
for i, v in pairs(player.Backpack:GetChildren()) do
if v:IsA("Tool") then
v:Destroy()
wait()
end
end
end
end)