Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why isn't my inventory system working?

My script is supposed to equip a flashlight every time I hit "f", and remove the flashlight if it is already equipped. It's not working. Can anyone help?

BP = script.Parent -- LocalScript is placed in the player's Backpack.
FL = BP.Flashlight
char = BP.Parent.Character -- Finds character.

Mouse = game.Players.LocalPlayer:GetMouse()

Mouse.KeyDown:connect(function(key)
	local a = FL:clone()
	if key == "f" and a.Parent == char then
		a:remove()
	else
		a.Parent = char
	end
end)

1 answer

✓ Accepted answer

You are checking if "a" is in the Player, while you have set a to clone the Flashlight. You should rather check if the Flashlight is in the player. I'm not sure if this will make a difference, but might as well try.

ALSO: You are only activating the function if the F key is pressed AND the Flashlight is in the character. If the Flashlight isn't there, the function isn't activating. This is what I suspect is the problem.

BP = script.Parent -- LocalScript is placed in the player's Backpack.
FL = BP.Flashlight
char = BP.Parent.Character -- Finds character.

Mouse = game.Players.LocalPlayer:GetMouse()

Mouse.KeyDown:connect(function(key)
    local a = FL:clone()
    if key == "f" then
	if char:FindFirstChild("Flashlight") then
        char:FindFirstChild("Flashlight"):Destroy() --remove is deprecated, use destroy.
    else
        a.Parent = char
    end
    end
end)

This should work, if not, let me know, and let me know if you have any errors in the output.

6 comments

0

It doesn't work and the output doesn't show any errors.

1

My bad, I forgot an end. Try now.

0

It still doesn't work. Now the output says "23:56:27.330 - Players.Player1.Backpack.InventoryScript:10: 'then' expected near 'if'".

1

Fixed. Again, I simply forgot to put a "then"" after an if statement. Update me what the result is.

0

It works now! Thanks so much!

1

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.