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

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

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)

3 comments

0

LocalPlayer is only available locally.

0

ok thanks but how can i make this script work without using LocalPlayer?

0

On PlayerAdded a player is passed into the arguments. Maybe you can grab that player instance's cash value instead?

1 answer

✓ Accepted answer

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

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