#Datastore script not working.

33 messages · Page 1 of 1 (latest)

topaz sand
#

Hello, I have a datastore script that is not working.

game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local plrID = "id_"..plr.UserId
    local Save1 = plr.Leaderstats.Cash
    
    local GetSaved = DataStore:GetAsync(plrID)
    if GetSaved then
        Save1.Value = GetSaved[1]
    else
        local NumberForSaving = {Save1.Value}
        DataStore:GetAsync(plrID, NumberForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    DataStore:GetAsync("id_"..plr.UserId, {plr.Leaderstats.Cash.Value})
end)```

The error states "Unable to cast Value to Object", which is put at line 12.
#

DataStore:GetAsync(plrID, NumberForSaving)

This is line 12

violet torrent
forest walrus
forest walrus
violet torrent
forest walrus
violet torrent
#

yes

#

took me about 2 days, 1 day to write then had to rewrite it again since it looked bad
then had to refix it again to update each value separately

#

and i dont use outside help for the scripts that i make

forest walrus
#

good for you

topaz sand
#

thanks for the help, ill try the script out later and hopefully it should work

topaz sand
# forest walrus I think you did a typo, in order to save a value to the datastore use SetAsync()...

i have fixed up all of the mispelled words but now it has a different error, this time leading to line 7:

game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local plrID = "id_"..plr.UserId
    local Save1 = plr.Leaderstats.Cash
    
    local GetSaved = DataStore:SetAsync(plrID)
    if GetSaved then
        Save1.Value = GetSaved[1]
    else
        local NumberForSaving = {Save1.Value}
        DataStore:SetAsync(plrID, NumberForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    DataStore:SetAsync("id_"..plr.UserId, {plr.Leaderstats.Cash.Value})
end)```
#

the error states "argument 2 missing or nil"

#

btw this is line 7: local GetSaved = DataStore:SetAsync(plrID)

#

oh i fixed it nevermind

#

i was only meant to rewrite line 12 typo not the others

#

it says there are no errors, however i do not know how to test it lol

topaz sand
#

oh wait

#

it works when im testing the game

#

but it shows an error that says unable to cast value to object at line 17

#

game.Players.PlayerRemoving:Connect(function(plr)

violet torrent
forest walrus
violet torrent
forest walrus
#

lmao happens sometimes

violet torrent
#

even for functions it happens to me if i misspelled it like lua funtion GetValue() end

forest walrus
#

I just type fu and press enter

#

half of my code is autocorrect anyways

violet torrent
#

welp time for me to try and adding Session Locking and im going to try and see how to make a Reload Data if its been wiped

topaz sand
#

do i just fix every setasync with getasync?