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

What can i do to make this datastore script work? it says LocalPlayer is a Nill Value.

Asked by 5 years ago
Edited 5 years ago

So i have made a datastore script that dosent work and when i start the game it says in the output that the LocalPlayer is a Nill value.

Script:

``local Datastore = game:GetService("DataStoreService") local DS1 = Datastore:GetDataStore("Statistics_of_PLR") local Cash = game.Players.LocalPlayer.leaderstats.Cash

game.Players.PlayerAdded:Connect(function(player) Cash.Value = DS1:GetAsync(player.UserId) or 0 DS1:GetAsync(player.UserId.Cash.Value)

Cash.Changed:Connect(function()
    print("Saving Data.....")
    DS1:SetAsync(player.UserId, Cash.Value)

end)

end)

game.Players.PlayerRemoving:Connect(function(player) DS1:SetAsync(player.UserId, player.leaderstats.Cash.Value) end)

0
LocalPlayer is only available locally. pidgey 548 — 5y
0
ok thanks but how can i make this script work without using LocalPlayer? DaggerSaber 14 — 5y
0
On PlayerAdded a player is passed into the arguments. Maybe you can grab that player instance's cash value instead? YaBoiToasterLord 80 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

You can only 'SetAsync' tables containing information. SetAsync(player.UserId, {Cash.Value}) is what you are looking for.

-- Save
local Data = {Cash.Value}
DataStorage:SetAsync(plr.UserId, Data)
-- Load
local Data = DataStorage:GetAsync(plr.UserId)
if Data then
    Cash.Value = Data[1]
else
    DataStorage:SetAsync(plr.UserId, {0})
end
Ad

Answer this question