Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is the variable considered nil?

I am working on my skills with datastores, but, for some reason, It gives me an error that a variable I set is nil.

local Player = game.Players.LocalPlayer
local DS = game:GetService("DataStoreService"):GetDataStore("Money")
function DataStore(player)
	local key = "user_" .. player.userId
	if DS:GetAsync(key) == nil then
	DS:SetAsync(key,0)
	print("key Created")
	else
	wait(1)
	player.leaderstats.Money.Value = DS:GetAsync(key)
	end
end
function Playeradded(player)
	local L = Instance.new("IntValue")
	local M = Instance.new("IntValue")	
	L.Name = "leaderstats"	
	L.Parent = player
	M.Parent = L
	M.Name = "Money"
end
function chatted(message)
	if message == "money" then
		Player.leaderstats.Money.Value = Player.leaderstats.Money.Value + 1
	end
end
game.Players.PlayerAdded:connect(Playeradded)
game.Players.PlayerAdded:connect(DataStore)
Player.Chatted:connect(chatted)

The error it gave me was: 12:41:03.560 - Workspace.Script:28: attempt to index local 'Player' (a nil value)

1 answer

✓ Accepted answer

Looks like your problem is that you are trying to access LocalPlayer within a server-sided Script. LocalPlayer can only be accessed through a LocalScript; if you try to through a Script, it will be equivalent to nil.

This discussion is preserved as it was. New answers, comments, and votes are closed.