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

Player is a nil value?

Asked by 5 years ago

So, I am making a script that when you touch a part, it gives you money. When I try to test it out, it says that players is a nil value.

Here's the code for the part:

script.Parent.Touched:connect(function() player = game:GetService("Players").LocalPlayer stats = players:findFirstChild("leaderstats") points = stats:findFirstChild("Points") points.Value = points.Value + 1 script.Parent:Destroy() print ("Coin completed") end)

Here's the leaderstats script:

game.Players.PlayerAdded:connect(function(player) stats = Instance.new("IntValue") stats.Parent = player stats.Name = "leaderstats" points = Instance.new("IntValue") points.Parent = stats points.Name = "Points" end)

Can someone please help me?

0
is it a local script (code in the part) EpicMetatableMoment 1444 — 5y
0
obviously DeceptiveCaster 3761 — 5y
0
I tried that, didn't work. BruzzTheHuzz -5 — 5y
0
Research goes a long way. https://developer.roblox.com/api-reference/property/Players/LocalPlayer To quote the Dev Hub, "For Scripts running on the server, this property is nil." TheeDeathCaster 2368 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

You:

 script.Parent.Touched:connect(function()  
player = game:GetService(“Players”).LocalPlayer  
stats = players:findFirstChild(“leaderstats”)  
points = stats:findFirstChild(“Points”)  
points.Value = points.Value + 1  
script.Parent:Destroy()  
print (“Coin completed”)  
end)

Me:

script.Parent.Touched:Connect(function(p)
    if p:IsA("BasePart") then
        local stats = game.Players:FindFirstChild(p.Parent.Name).leaderstats -- player's stats
        stats.Points.Value = stats.Points.Value + 1
        script.Parent:Destroy()
        print("Coins completed.")
    end
end

as you see, you can't get a player from a normal script unless you use events. ~Enjoy your working code, k bye

0
Nailed it AswormeDorijan111 531 — 5y
Ad

Answer this question