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

Data Store script not saving values? [Help!]

Asked by 5 years ago
Edited 5 years ago

This data store script doesn't work for some reason. Can you guys check it out please. I have put print() after if nil then and it did not print, this means that the data store is saving but it's not saving the right value.

Script location = ServerScriptService

local xp = game:GetService("DataStoreService"):GetDataStore("XP")

local function Xp(key, newValue)
    local suc, res = pcall(function()  return xp:SetAsync(key, newValue) end)
    if suc then 
        return true
    end
end

game:GetService('Players').PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local experience = Instance.new('IntValue')
        experience.Name = 'XP'
        experience.Value = xp:GetAsync(player.UserId)
        if nil then
            -- I put the print() in here
            experience.Value = 0
        end
        while wait(30) do
            Xp(player.UserId, player.XP.Value)
        end
    end)
end)

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

You forgot to set the parent, I gave some modified. errors: Parent not set. You can not put if nil then you must specify the variable / object. You do not need to use player.CharacterAdded. It is not necessary to create a save function.

If it does not work in Roblox Studio you must enable the API, go to the page of your place, go to Configure Game and enable Studio Access to API Services, click save and that's it.

Here is the script done:

local xp = game:GetService("DataStoreService"):GetDataStore("XP")

local startvalue = 0 -- Set start XP

game:GetService('Players').PlayerAdded:Connect(function(player)
    local async = xp:GetAsync(player.UserId) -- Get Saved Value
    local leaderstats = Instance.new('IntValue', player) -- Create a leaderstats
    leaderstats.Name = 'leaderstats'
    local experience = Instance.new('IntValue', leaderstats) -- Create XP value
    experience.Name = 'XP'
    if async then -- If found save then
        experience.Value = xp:GetAsync(player.UserId) -- Get XP Saved
    else
        experience.Value = startvalue -- Set start value
        wait()
        xp:SetAsync(player.UserId, experience.Value) -- Save XP
    end
    experience.Changed:Connect(function() -- If XP changed save value
        xp:SetAsync(player.UserId, experience.Value) -- Save XP
    end)
end)

If you want to make some changes you can do, however it is not recommended to add a loop to save the value.

Remember, for save you need to change value with server, for this use RemoteEvent

0
I actually do need player.CharacterAdded, because i have other data stores i just took one of them out of my script Hypoxic1 8 — 5y
0
Create a new script and datastore key to store the other value. yHasteeD 1819 — 5y
0
if you want me try to fix, send all code. with another data store yHasteeD 1819 — 5y
0
I disabled my Data Store script and pasted yours in a new script, it didn't work Hypoxic1 8 — 5y
View all comments (7 more)
0
what error? (remember if you using "XP" data store on two scripts, you need to change the key in one of the two) yHasteeD 1819 — 5y
0
Remember, only can save the values if server change. if the client change not save anything, for this use Remote Event or Server Script. yHasteeD 1819 — 5y
0
No Error Hypoxic1 8 — 5y
0
If no error you need to change the value with server, For this use RemoteEvent or Server Script. yHasteeD 1819 — 5y
0
Done that didn't work Hypoxic1 8 — 5y
0
I do not know then, because I tried the script and it works perfectly for me. (If you already used the DataStore "XP" change for "XP-0" or anything number...) yHasteeD 1819 — 5y
0
I figured it out It was that i wasn't changing the value on the server side but i thought i did, but i guess not (btw i tested with my original code and it works perfectly) Hypoxic1 8 — 5y
Ad

Answer this question