Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why isn't my inventory system working?

Asked by
Nifer2 174
9 years ago

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

Log in to vote
1
Answered by 9 years ago

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.

0
It doesn't work and the output doesn't show any errors. Nifer2 174 — 9y
1
My bad, I forgot an end. Try now. SlickPwner 534 — 9y
0
It still doesn't work. Now the output says "23:56:27.330 - Players.Player1.Backpack.InventoryScript:10: 'then' expected near 'if'". Nifer2 174 — 9y
1
Fixed. Again, I simply forgot to put a "then"" after an if statement. Update me what the result is. SlickPwner 534 — 9y
View all comments (2 more)
0
It works now! Thanks so much! Nifer2 174 — 9y
1
No problem! SlickPwner 534 — 9y
Ad

Answer this question