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

datastore is not saving data?

Asked by 4 years ago
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

local player = game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local collectibles = Instance.new("IntValue")
    collectibles.Name = "Collectibles"
    collectibles.Parent = leaderstats
    collectibles.Value = workspace.Collectibles.Value

    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Parent = leaderstats
    level.Value = 1

    player.RespawnLocation = workspace.Level1.L1Spawn

    local playerUserID = "Player_"..player.UserId

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserID)
    end)

    if success then
        collectibles.Value = data
    end

end)

game.Players.PlayerRemoving:Connect(function()
    local playerUserID = "Player_"..player.UserId

    local data = player.leaderstats.collectibles.Value

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserID, data)
    end)

    if success then
        print("data saved")
    else
        print("data not saved")
        warn(errormessage)
    end

end)
0
Are there any errors in the output, and if so, what line is throwing the error? killerbrenden 1537 — 4y
0
no errors Imgood543 21 — 4y
0
Also, could you explain your problem in more detail instead of saying it doesn't work and posting your script? killerbrenden 1537 — 4y
0
the value isnt saving Imgood543 21 — 4y
View all comments (6 more)
0
remove the local player = on line 4 as it is pointless User#5423 17 — 4y
0
i tried again and got this error "UserId is not a valid member of RBXScriptConnection" Imgood543 21 — 4y
0
How are you testing whether or not it saves? If you play the game and manually edit your intvalue, it won't save. However, if you write up a script that changes to intvalue, it does save. akatak_prime 48 — 4y
0
i have a collectible coin that I tried picking up Imgood543 21 — 4y
0
Also, you should add "player" as a parameter for the leave function akatak_prime 48 — 4y
0
As akatak_prime said add in the player paramater as your code will try and read the varable on line 4. Lua will take the variable that has the closest scope if multiple variable with the same name exist. User#5423 17 — 4y

2 answers

Log in to vote
-1
Answered by 4 years ago

I fixed the problem, was just some minor capitalization errors on my part.

Ad
Log in to vote
-2
Answered by 4 years ago

First of all Remember to turn API Services on, so you need to publish the game and go to game settings and turn API Services on

Second of all it does not work in roblox studio

Third of all your good to go!

0
DataStore very much does work in studio. Utter_Incompetence 856 — 4y
0
it doesn't seem to be working in game either Imgood543 21 — 4y
0
it works in studio. greatneil80 2647 — 4y

Answer this question