Ad
Log in to vote
0

How to check a value in the player?

Asked by 2 months ago
Edited 2 months ago

I'm making a thing where i t makes a folder called Inventory inside the player and a intValue called Log and when they click this button it checks if they have 5 or more logs it prints the player name has 5 or more logs but it didnt work Instead i got a error: Players.googlpeopel15.PlayerGui.InvGUI.Frame.LogWall.PurchaseHandler:3: attempt to index nil with 'Inventory' - Client - PurchaseHandler:3 12:15:33.742

Here is Inventory adding code. (Server script in ServerScriptService)

game.Players.PlayerAdded:Connect(function(player)
    local Inventory = Instance.new("Folder")
    Inventory.Parent = player
    Inventory.Name = "Inventory"

    local Log = Instance.new("IntValue")
    Log.Parent = Inventory
    Log.Value = 0
    Log.Name = "Log"
end)

The Code that is causing errors (LocalScript in InvGUI which is in StarterGUI)


script.Parent.MouseButton1Click:Connect(function(player) local logamount = player.Inventory.Log if logamount.Value >= 5 then print(player.Name "Has 5 or more then 5 logs") else print(player.Name "Has less than 5 logs") end end)

2 answers

Log in to vote
0
Answered by 2 months ago

I dont think MouseButton1Click has any default arguments. Since this is a local script, you can access the player with LocalPlayer.

local logamount = game.Players.LocalPlayer.Inventory.Log
0
True, MouseButton1Click has no paramaters. deeskaalstickman649 475 — 2mo
0
Thanks for the confirmation, I wasn't too sure JustinWe12 694 — 2mo
Ad
Log in to vote
0
Answered by 2 months ago

Instead of:

script.Parent.MouseButton1Click:Connect(function(player)
    local logamount = player.Inventory.Log
    if logamount.Value >= 5 then
        print(player.Name "Has 5 or more then 5 logs")
    else
        print(player.Name "Has less than 5 logs")
    end
end)

Try:


local player = game.Players.LocalPlayer script.Parent.MouseButton1Click:Connect(function() local logamount = player.Inventory.Log if logamount.Value >= 5 then print(player.Name "Has 5 or more then 5 logs") else print(player.Name "Has less than 5 logs") end end)

Answer this question