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

Why is the variable considered nil?

Asked by 8 years ago

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

Log in to vote
1
Answered by
Unclear 1776 Moderation Voter
8 years ago

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.

Ad

Answer this question