Hello everyone, I’ve been having a quite frustrating issue with some logic in my code recently, and despite me scripting on roblox for over 4 years, I cannot seem to wrap my head around how to get this code to work.
Basically, I have some code that prevents the player from equipping tools while crouching. The problem is, the player can still somehow always find a way to equip the tool and crouch at the same time.
I am almost certain the solution lies within Equip(), because I tested HandleCrouchRemove() with prints and everything seemed to work fine.
Any help would be greatly appreciated!!!
Logic:
Player equips a tool, and then crouches, the weapon gets removed, but if they stand up it gives the tool back.
Player equips a tool, crouches, equips a second tool, then stands up, it gives them the second tool, as that's the new item.
Player equips a tool, crouches, equips nothing, then stands up, it gives them nothing.
Player equips nothing, crouches, equips tool, then stands up, it gives tool
Player equips tool, crouches, then unequips tool, then re-equips tool, it doesn't give them the tool until they stand up.
Code (stripped)
local PlayerCrouchRemoves = {}
function Equip(Player,CurrentTool,PreviousTool)
-- Logic?
if CurrentTool ~= nil then
CurrentTool.Parent = Player.Character
end
if PreviousTool ~= nil and PreviousTool.Parent == Player.Character then
PreviousTool.Parent = Player.Backpack
end
end
function HandleCrouchRemove(Player,Parameter1)
if Parameter1 == true then
for _,Object in pairs(Player.Character:GetChildren()) do
if Object:IsA("Tool") then
Object.Parent = Player.Backpack
PlayerCrouchRemoves[Player] = Object
return
end
end
PlayerCrouchRemoves[Player] = true
elseif Parameter1 == false then
if (PlayerCrouchRemoves[Player] ~= true and PlayerCrouchRemoves[Player] ~= nil) then
PlayerCrouchRemoves[Player].Parent = Player.Character
end
PlayerCrouchRemoves[Player] = nil
end
end
Events.HandleCrouchRemove.OnServerEvent:Connect(HandleCrouchRemove)