Need help fixing inventory script

Hello, im making a inventory system and im trying to make if where if u press F and the LastToolEquipped Value =='s Nil then ur current light unequips to nothing, but if it doesn’t == nil then your last tool equips, currently i only have it where if u press F it unequips to nothing, when i tried to script it it made the whole thing not work. If you confused tell me and i will explain better

local player = game.Players.LocalPlayer
local character = player.Character
local selected = script.Parent.Parent.Frame.Handler.Selected
local LastEquippedTool = script.Parent.Parent.Frame.Handler.LastEquippedTool


Plr:GetMouse().KeyDown:Connect(function(E)
	if E == "f" then
		local equipped = script.Parent.Parent.Frame.Handler.Equipped
		local LastEquipped = script.Parent.Parent.Frame.Handler.LastEquippedLight
		if script.Parent.Parent.Delay.Value == false then
	       if script.Parent.Equipped.Value == true then
				if equipped.Value ~= LastEquipped.Value then
					script.Parent.Parent.Delay.Value = true
				character.Humanoid:UnequipTools()
				script.Parent.Parent.Frame.Handler.Equipped.Value = nil
				script.Parent.Equipped.Value = false
					wait(1)
					script.Parent.Parent.Delay.Value = false
				script.Parent.Unequipped.Disabled = false
				script.Disabled = true
			else
				local equipped1 = script.Parent.Parent.Frame.Handler.Equipped
					local LastEquipped1 = script.Parent.Parent.Frame.Handler.LastEquippedLight
					if script.Parent.Parent.Delay.Value == false then
				if equipped1.Value == LastEquipped1.Value then
							character.Humanoid:UnequipTools()
							script.Parent.Parent.Delay.Value = true
					script.Parent.Parent.Frame.Handler.Equipped.Value = nil
					        script.Parent.Parent.Frame.Frame.Equip.Text = "Equip"
					script.Parent.Equipped.Value = false
							wait(1)
							script.Parent.Parent.Delay.Value = false
							script.Parent.Unequipped.Disabled = false
					script.Disabled = true
				               end
			              	end
			        	end
					end
				end
	        end
	    end)

Sorry I explained this very bad, I’m trying to make it where if u equip a tool the tool will go into a object value, if you Click F which F equips a light source u equip ur lantern but here’s what I want to change, I want to make it where if the value for the LastToolEquipped == false then when u press F it equips to nothing, but if it doesn’t == nil and u Press F then instead of equipping to nothing it equips to the LastToolEquipped

I tried to add this into the script but it ruined the whole thing, any help?

https://developer.roblox.com/en-us/api-reference/function/Humanoid/EquipTool

Why not use userinputsrvc? Its more easier. Mouse.KeyDown is d3precated.

I know how to unequip the tool and equip it it’s just idk where to put it in the script

https://developer.roblox.com/en-us/api-reference/event/Mouse/KeyDown

Not deprecated, just superseded.

Can you comment the area in the code where you would like the flashlight to be equipped? I’ll see what I can do.

The script I put in the post fully works just without the feature I wanted, if u press F ur lantern equips, press it again your lantern goes away all I want to change is if there is a value for LastEquipedTool then it equips it instead of ur lantern going away, if there isn’t a value then the script works the exact same as u press F ur lantern equips, press it again your lantern goes away, idk where to put the If statement in the script tho, last time I tried the script no longer works. Do you get what I’m saying?

character.Humanoid:EquipTool(LastEquipped1.Value)

Shouldn’t this be all you need if the script is working as you stated? Assuming “LastEquipped1” is an ObjectValue instance of which its value is a reference to the Tool instance itself which was most recently equipped.

No, if you shouldn’t put that as the equipped if there is a value for LastEquippedTool

Can you comment the area in the code where you would like the flashlight to be equipped? I’ll see what I can do.

I’m just going to repeat my earlier message as I have no idea what you’re saying.

ah nvm ill try to figure it out