Help with Leaderstats with DataStore Script

I’m creating a game where I want players to kill people for cash, and then save to the DataStore.

For some reason though, it won’t save, which means there’s obviously something wrong.

Leaderstats Script With Kill For Cash
game.Players.PlayerAdded:Connect(function(player) -- Leaderstats Setup
      local leaderstats = Instance.new("Folder")
      leaderstats.Name = "leaderstats"
      leaderstats.Parent = player

      local money = Instance.new("NumberValue") -- The value I'm trying to record
      money.Name = "Money"
      money.Value = 0
      money.Parent = leaderstats

player.CharacterAdded:Connect(function(Character)
     Character.Humanoid.Died:Connect(function(Died)
         local creator = Character.Humanoid:FindFirstChild("creator")
         local leaderstats = creator.Value:FindFirstChild("leaderstats")
         if creator ~= nil and creator.Value ~= nil then
            leaderstats.Money.Value = leaderstats.Money.Value = + 10
          end
       end)
     end)
end)


DataStore Script (Separate Script)
local DataStore = game.GetService("DataStoreService"):GetDataStore
game.Players.PlayerAdded:Connect(function(player)
     wait()
     local plrkey = "id_"..plr.userId
     local save1 = plr.leaderstats.Money

     local GetSaved = DataStore:GetAsync(plrkey)
     if GetSaved then
     save1.Value = GetSaved[1]
     else
         local NumberForSaving = {save1.Value}
         DataStore:GetAsync(plrkey, NumberForSaving)
      end
end)

game.Players.PlayerRemoving:Connect(function(plr)
     DataStore:SetAsync("id_"..plr.userId, {plr.leaderstats.Money.Value})
end)

Both scripts are in ServerScriptServer, and I have " Studio Access to API Services " and " Allow HTTP Requests " enabled in Game Settings, but for reason when I kill someone, and earn the extra cash, it won’t save when I rejoin.

Can someone tell me what I’m doing wrong?

I apologize if this has already been posted somewhere else on the DevForum. I’m relatively new to the DevForums, so I could’ve missed a post that had the same thing as this.

You have to give your DataStore a name:

local DataStore = game.GetService("DataStoreService"):GetDataStore("MyDataStore") -- call it whatever you want
1 Like

Thank you! :slight_smile:

Do I have to change anything else on the script?

Instead of number value, you could use int value.

Edit: Not an error, just a recommendation

I would suggest you wrap the DataStore calls in pcall so you can catch any errors etc:

local success, err =  pcall(function() GetSaved=DataStore:GetAsync(plrkey) end) 
if not success then warn(err) end
1 Like

Also you cant give 2 parameters to getasync. You’ve to change it to SetAsync

1 Like

So is this good then?

local DataStore: game.GetService("DataStoreService"):GetDataStore("StoreData")
game.Players.PlayerAdded:Connect(function(player)
    wait()
    local plrkey = "id_"..plr.userId
    local save1 = plr.leaderstats.Money

    local GetSaved = DataStore:GetAsync(plrkey)
    if GetSaved then
    save1.Value = GetSaved[1]
    else
        local NumberForSaving = {save1.Value}
        DataStore:SetAsync(plrkey, NumberForSaving)
	local success, err =  pcall(function() GetSaved=DataStore:GetAsync(plrkey) end) 
if not success then warn(err) 
      end
   end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    DataStore:SetAsync("id_"..plr.userId, {plr.leaderstats.Money.Value})
end)

local Players = game:GetService("Players")
local DataStore = game.GetService("DataStoreService"):GetDataStore("StoreData")
repeat wait()
until Players

game.Players.PlayerAdded:Connect(function(player)
	local plrkey = "id_"..plr.userId
	local save1 = plr.leaderstats.Money
	local GetSaved = DataStore:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
	else
		local NumberForSaving = {save1.Value}
		DataStore:SetAsync(plrkey, NumberForSaving)
		local success, err =  pcall(function()
			GetSaved = DataStore:GetAsync(plrkey)
		end)
		if not success then
			warn(err) 
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, err =  pcall(function()
		DataStore:SetAsync("id_"..plr.userId, {plr.leaderstats.Money.Value})
	end)
	if not success then
		warn(err)
	end
end)

Thank you :smiley:

I appreciate all of your help.

i have this model that has a datascript i think it should help

Let me try it out. Thanks for the help!

I’m assuming you got the Kill-Death Script from the Roblox CTP Model?

yep, i use the model for it, but the MainDatastoreScript is the only thing you wanna use when you want a datastore script Just Place it in the ServerScriptService

I came up with this script and it worked:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want

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

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

	local FlagBucks = Instance.new('NumberValue', leaderstats)
	FlagBucks.Name = "FlagBucks"
	FlagBucks.Value = 0

	local value1Data = FlagBucks

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
	end)

	if s then
		FlagBucks.Value = value1Data --setting data if its success
	else
		game:GetService("TestService"):Error(e)  --if not success then we error it to the console
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.FlagBucks.Value) --setting data
	end)
	if not s then game:GetService("TestService"):Error(e) 
	end
end)

Well, I figured it out a while ago, but I forgot to mark it as solution. Thanks for your help everyone!

1 Like