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

How can I fix this datastore so that the values save?

Asked by 7 years ago

Please can someone tell me why this isn't working and how I can fix it.

A regular script in ServerScriptService

The values are given to the player but aren't saved.

local DSS = game:GetService("DataStoreService")
local leveldatastore = DSS:GetDataStore("LevelNumber")

game:GetService("Players").PlayerAdded:connect(function(player)
    local playerid = player.UserId
    local level = Instance.new("NumberValue",player)
    level.Name = "level"
    local levelfound = leveldatastore:GetAsync(playerid)
    if levelfound == true then
        level.Value = levelfound[1]
    else
        local leveltobesaved = {level.Value}
        leveldatastore:SetAsync(playerid,leveltobesaved)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    local playerid = player.UserId
    local leveltobesaved = {player:WaitForChild("level").Value}
    leveldatastore:SetAsync(playerid,leveltobesaved)
end)

1 answer

Log in to vote
0
Answered by 7 years ago
local DSS = game:GetService("DataStoreService")
local leveldatastore = DSS:GetDataStore("LevelNumber")

game:GetService("Players").ChildAdded:connect(function(player)
wait(1)
    local playerid = player.UserId
    local level = Instance.new("NumberValue",player)
    level.Name = "level"
    local levelfound = leveldatastore:GetAsync(playerid)
    if levelfound == true then
        level.Value = levelfound[1]
    else
        local leveltobesaved = {level.Value}
        leveldatastore:SetAsync(playerid,leveltobesaved)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    local playerid = player.UserId
    local leveltobesaved = {player:WaitForChild("level").Value}
    leveldatastore:SetAsync(playerid,leveltobesaved)
end)

0
still doesn't work for some reason e.e Citranix 19 — 7y
Ad

Answer this question