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

How to make my Datastore load the player's Cash?

Asked by 4 years ago

Script so when the player joins the game, it gives the leaderstats folder + the Cash value inside

01function onPlayerEntered(newPlayer)
02    wait()
03    local stats = Instance.new("Folder")
04    stats.Name = "leaderstats"
05 
06    local score = Instance.new("IntValue")
07 
08    score.Name = "Cash"
09    score.Value = 0
10 
11    score.Parent = stats   
12    stats.Parent = newPlayer
13end
14 
15game.Players.ChildAdded:connect(onPlayerEntered)

Datastore Script

01local ds = game:GetService("DataStoreService"):GetDataStore("CashData")
02 
03game.Players.PlayerAdded:Connect(function(plr) --Load
04 
05    wait(0.2)
06 
07    local plrkey = "id_"..plr.userId
08    local leaderstats = plr:WaitForChild("leaderstats")
09    local cash = leaderstats:WaitForChild("Cash")
10 
11    local GetSaved = ds:GetAsync(plrkey)
12 
13    if GetSaved then
14        cash.Value = GetSaved[1]
15    else
View all 29 lines...

[Error is from the Datastore script]:

"ServerScriptService.CashDatastore:14: attempt to index number with number"

1 answer

Log in to vote
0
Answered by 4 years ago

Not sure if that's the problem, but line 17 (data store script) contains GetAsync(), and it seems like you want to save, not load, so change it to SetAsync(). That might be the problem (No data). But I am not sure since you get Attempt to index number with number, and not nil.

Ad

Answer this question