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

How to check a value in the player?

Asked by 3 years ago
Edited 3 years 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)

01game.Players.PlayerAdded:Connect(function(player)
02    local Inventory = Instance.new("Folder")
03    Inventory.Parent = player
04    Inventory.Name = "Inventory"
05 
06    local Log = Instance.new("IntValue")
07    Log.Parent = Inventory
08    Log.Value = 0
09    Log.Name = "Log"
10end)

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

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

2 answers

Log in to vote
0
Answered by 3 years ago

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

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

Instead of:

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

Try:

01local player = game.Players.LocalPlayer
02 
03script.Parent.MouseButton1Click:Connect(function()
04    local logamount = player.Inventory.Log
05    if logamount.Value >= 5 then
06        print(player.Name "Has 5 or more then 5 logs")
07    else
08        print(player.Name "Has less than 5 logs")
09    end
10end)

Answer this question