So I have this inventory system script that detects when I hit certain keys (1, 2, 3, 4, etc) and puts the tool assigned to that hotbar slot into the character (forcing them to hold it) and removed and placed back into the backpack when toggled again (or swapped to different hotbar slot). For some reason, when tools are removed from the backpack and the game entirely (deleted), the script will RE-ADD them back into the game and into the character (when assigned hotbar is toggled), then when de-equipped it will be back in the backpack as if nothing happened.
Anybody know why this is happening? I suspect the problem is something to do with the server side script and I’ve been tweaking for the past hour over it.
Part I think is broken:
-- Get Services
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Anti-cheat assets
local AntiHackFunni = ServerStorage:WaitForChild("AntiHackFunni")
local Getout = AntiHackFunni:WaitForChild("GETOUT")
-- Define a function to handle slot selection
local function onSlotToggle(player, currentSlot)
local backpack = player:FindFirstChild("Backpack")
if not backpack then
warn("Backpack not found for player: " .. player.Name)
return
end
-- Check if a tool is already equipped in the character
local equippedTool = nil
for _, tool in ipairs(player.Character:GetChildren()) do
if tool:IsA("Tool") then
equippedTool = tool
break
end
end
-- Unequip the currently equipped tool if its slot matches the selected slot
if equippedTool then
local assignedSlot = equippedTool:FindFirstChild("AssignedSlot")
if assignedSlot and assignedSlot.Value == currentSlot then
print("Unequipping tool: " .. equippedTool.Name)
equippedTool.Parent = backpack
return
else
print("Unequipping tool: " .. equippedTool.Name)
equippedTool.Parent = backpack
end
end
-- Search for the tool to equip in the backpack
local toolToEquip = nil
for _, tool in ipairs(backpack:GetChildren()) do
local assignedSlot = tool:FindFirstChild("AssignedSlot")
if tool:IsA("Tool") and assignedSlot and assignedSlot.Value == currentSlot then
toolToEquip = tool
break
end
end
-- Equip the tool if found, otherwise warn about the missing tool
if toolToEquip then
print("Equipping tool: " .. toolToEquip.Name)
toolToEquip.Parent = player.Character
else
warn("No tool found in slot " .. tostring(currentSlot) .. " for player: " .. player.Name)
end
end
-- DISPLAY ITEMS
local DisplayItem = ReplicatedStorage:WaitForChild("DisplayItem")
local function UpdateItems(player)
print("Updating items for", player.Name)
local PlayerGui = player:WaitForChild("PlayerGui")
local InvAndHotbar = PlayerGui:WaitForChild("InvAndHotbar")
local Hotbar = InvAndHotbar:WaitForChild("Hotbar")
for _, slot in ipairs(Hotbar:GetChildren()) do
local AssignmentNum = slot:FindFirstChild("AssignmentNum")
if not AssignmentNum then
continue
end
for _, tool in ipairs(player.Backpack:GetChildren()) do
local AssignedSlot = tool:FindFirstChild("AssignedSlot")
if tool:IsA("Tool") and AssignedSlot then
if AssignedSlot.Value ~= AssignmentNum.Value then
continue
end
print(tool.Name, "is assigned in Slot", AssignmentNum.Value)
AssignmentNum.Parent.Image = tool.TextureId
end
end
end
end
local function MonitorBackpack(player)
local backpack = player:WaitForChild("Backpack")
-- Detect when items are added to the backpack
backpack.ChildAdded:Connect(function()
UpdateItems(player)
end)
-- Detect when items are removed from the backpack
backpack.ChildRemoved:Connect(function()
UpdateItems(player)
end)
end
-- Trigger UpdateItems when the remote event is fired
DisplayItem.OnServerEvent:Connect(function(player)
UpdateItems(player)
end)
-- Monitor backpack changes for each player
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
MonitorBackpack(player)
end)
end)