Attempt to index global 'player' (a nil value)

Hello. I got a scripting book for beginners. My issue is that i get Workspace.Part.Script:3: attempt to index global ‘player’ (a nil value)
in this Script:
script.Parent.Touched:connect(function()
player = game:GetService(“Players”).LocalPlayer
stas = player:findFirstChild(“leaderstats”)
points = stas:findFirstChild(“Points”)
points.Value = points.Value + 1
script.Parent:Destroy()
end)
It might be because the book is a bit outdated. I already tried putting it into a Local Script.

The problem here is that you’re trying to find game.Players.LocalPlayer, which won’t exist server-side. Instead, the Touched event provides as an argument the part which touched it, and I’d suggest going off that by mapping it onto a player (using the GetPlayerFromCharacter method):

script.Parent.Touched:connect(function(hit) -- hit will be the object that touched your part
local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- map the touching object into a player
if not player then return end -- If the touching part wasn't a player, exit out

-- Otherwise - go through the same code you had from this point forward
stas = player:findFirstChild(“leaderstats”)
points = stas:findFirstChild(“Points”)
points.Value = points.Value + 1
script.Parent:Destroy()
end)
2 Likes