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

I have a script to save a players data but it isn't working?

Asked by
sheepposu 561 Moderation Voter
5 years ago
Edited 5 years ago

I have a saving script to save various things. I've looked through it multiple times and I can't tell why it won't work. Thanks in Advance!

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("-xBuckDatax-")

-- Making the saving inside a function it shortened your code by a bit.
local function Save(player)
    local key = "Player_"..player.UserId

    local save = {
        ["SeerBux"] = player.leaderstats["SeerBux"].Value,
        ["SpdWaitTime"] = player.WaitTime.WaitTimeSpd.Value,
        ["StrWaitTime"] = player.WaitTime.WaitTimeStr.Value
    }

    --saves their key and money to the data store
    local success, err = pcall(function()
        DataStore:SetAsync(key, save)
    end)

    if not success then
        warn("Failed to over-write data"..tostring(err))
        return
    end
end

-- I made this a function so it can be called whenever
local function Load(player)
    -- saves the player Id as key
    local key = player.UserId

    local moneyAlready

    local success, err = pcall(function()
        moneyAlready = DataStore:GetAsync(key)
    end)

    if not success then
        warn("Failed to read data"..tostring(err))
        return
    end

    if moneyAlready then
        player.leaderstats.SeerBux.Value = moneyAlready.SeerBux
        player.WaitTime.WaitTimeSpd.Value = moneyAlready.SpdWaitTime
        player.WaitTime.WaitTimeStr.Value = moneyAlready.StrWaitTime
    else
        Save(player)
    end
end

Players.PlayerAdded:Connect(Load)
Players.PlayerRemoving:Connect(Save)
0
Did you give you stats values if a player is new? CaptainD_veloper 290 — 5y
0
I have a leaderboard and when the game launches if they have stats this script will edit the values of the default settings sheepposu 561 — 5y
0
in the Load function the key is the player's userid alone, but you set the table's key as "Player_"..player.UserId in the save function EXpodo1234ALT 18 — 5y
0
thx sheepposu 561 — 5y

1 answer

Log in to vote
0
Answered by
BRAEGON -8
5 years ago
Edited 5 years ago

As far as I know, you can't save multiple values to the same DataStore. You have to manually assign them. I can't really tell what you're trying to save but try something like this

game:GetService("Players") game.Players.PlayerAdded:Connect(function(newPlayer)

local DataStore = game:GetService("DateStoreService") local Money = DataStore:GetDataStore("PlayerMoneyService") --just a save value for instance

you could use

v.Value = Money:GetAsync(newPlayer.UserId) or 100

and then you could set it with

local x = 250 -- whatever money the player has in the game

Money:SetAsync(newPlayer.UserId, x.Value)

x.Changed:Connect(function() Money:SetAsync(newPlayer.UserId, x.Value) end)

end)

hope this helps. you might want to make a script that places a number value inside of the player (game.Player) and then use that as x. Then you can use touch scripts or other scripts to change to players money value, or X. When the player's Money value changes the function will be triggered and the datastore will save. Hope this helps

also, I would ditch the function

I redid the code in the studio and the final thing should look like this

game:GetService("Players") local DataStore = game:GetService("DataStoreService") local Money = DataStore:GetDataStore("PlayerMoneyService") --just a save value for instance game.Players.PlayerAdded:Connect(function(newPlayer)

local PMoney = Instance.new("NumberValue", newPlayer) -- makes a value and puts it in the player --(game.Player)

PMoney.Value = Money:GetAsync(newPlayer.UserId) or 100

Money:SetAsync(newPlayer.UserId, PMoney)

PMoney.Changed:Connect(function() Money:SetAsync(newPlayer.UserId, PMoney.Value) end)

end)

changed the players money get the players name from whatever function they triggered and search for them in game.Players by using their name

Ad

Answer this question