Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a reason why the datastore not working on server?

local ds = game:GetService("DataStoreService")
local cashDs = ds:GetDataStore("CashDataStore")
game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	local cash = Instance.new("NumberValue", leaderstats)
	cash.Name = "Cash"
	cash.Parent = leaderstats
	local data = cashDs:GetAsync(tostring(plr.UserId))
	if data ~= nil then
		cash.Value = data
		cashDs:SetAsync(tostring(plr.UserId), cash.Value)
	else
		data = 0
		cash.Value = data
		cashDs:SetAsync(tostring(plr.UserId), cash.Value)
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	cashDs:SetAsync(tostring(plr.UserId), plr.leaderstats.Cash.Value)
end)

It always gives 0 even when I increment it.

2 answers

Try using my ds2 it will be easier once you understand it

local ds2 = require(1936396537)
local plrs = {}
function playeradded(player)
    local cashDs = ds2("Cash", player)
    


    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player 
    
    local cash = Instance.new("IntValue")
    cash.Name = "Cash" -- currency being used
    cash.Value = cashDs:Get(0) -- Starting value of cash
    cash.Parent = leaderstats
    



end

function save(player)
    local cashDs = ds2("Cash", player)
    cashDs:Set(player.leaderstats.Cash.Value)

end
game.Players.PlayerRemoving:Connect(function(player)
    save(player)
end)
game.Players.PlayerAdded:Connect(function(player)
    table.insert(plrs, #plrs + 1, player.Name)
    playeradded(player)
end)

function check(tbl, thing)
    for i, v in pairs(tbl) do
        if v == thing then
            return true
        end
    end
end

for i, v in pairs(game.Players:GetPlayers()) do
    if not check(plrs, v.Name) then
        table.insert(plrs, #plrs + 1, v.Name)
        playeradded(v)
    end
end

I made some points in the script for you if it helps accept also if this doesnt work you must turn https request on in your settings cause that is your problem if so

nvm guys I fixed it I was incrementing on the client

This discussion is preserved as it was. New answers, comments, and votes are closed.