Why doesn't it saves the stats(Api is on)

So it doesn’t save the stats even thow i have api on and I am changing the Values on server, why is that?

local dataStore = game:GetService("DataStoreService")
local KillStore = dataStore:GetDataStore("KillStore")
local killEvent = game.ReplicatedStorage.KillEvent
local function leaderstats(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local kill = Instance.new("NumberValue")
	kill.Name = "Kill"
	kill.Parent = leaderstats

	local death = Instance.new("NumberValue")
	death.Name = "Death"
	death.Parent = leaderstats

	local cash = Instance.new("NumberValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats

	local Once = Instance.new("BoolValue")
	Once.Name = "Once"
	Once.Parent = plr
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Parent = leaderstats
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = plr

	XP:GetPropertyChangedSignal("Value"):Connect(function()
		if XP.Value >= 100 * level.Value then
			level.Value += 1
		end
	end)
	plr.CharacterAdded:connect(function(char)
		local humanoid
		repeat
			humanoid = char:FindFirstChild("Humanoid")
			wait()
		until humanoid
		humanoid.Died:connect(function() 
			death.Value = death.Value + 1 
			local tag = humanoid:FindFirstChild("creator") 
			if tag then  
				local killer = tag.Value  
				if killer and killer ~= plr then   
					local killername = killer.Name
					killer.leaderstats.Kill.Value = killer.leaderstats.Kill.Value + 1
					killer.leaderstats.Cash.Value = killer.leaderstats.Cash.Value + 100
					killer.XP.Value = killer.XP.Value + 50
					killEvent:FireAllClients(plr,killername)
				end   
			end 
		end)
	end)

end

local function Save(plr)
	local data
	local data2
	local data3
	local data4
	local data5
	local data6
	local success,errormessage = pcall(function()
		data = KillStore:GetAsync(plr.UserId.."-kill")
		data2 = KillStore:GetAsync(plr.UserId.."-death")
		data3 = KillStore:GetAsync(plr.UserId.."-cash")
		data4 = KillStore:GetAsync(plr.UserId.."-once")
		data5 = KillStore:GetAsync(plr.UserId.."-level")
		data6 = KillStore:GetAsync(plr.UserId.."-XP")
	end)

	if success then
		plr:WaitForChild("leaderstats").Kill.Value = data
		plr:WaitForChild("leaderstats").Death.Value = data2
		plr:WaitForChild("leaderstats").Cash.Value = data3
		plr:WaitForChild("Once").Value = data4
		plr:WaitForChild("leaderstats").Level.Value = data5
		plr:WaitForChild("XP").Value = data6
	else
		print("There was an error while getting your data")
		warn(errormessage)
	end
end

local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("Tools")
local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SaveData")




local function ToolData1(plr)
	local ToolData = SaveData:GetAsync(plr.UserId)

	local BackPack = plr:WaitForChild("Backpack")
	if ToolData ~= nil then
		for i,v in pairs(ToolData)do
			if ToolFolder:FindFirstChild(v) and BackPack:FindFirstChild(v) == nil then
				ToolFolder[v]:Clone().Parent = BackPack
			end
		end
	end

	plr.CharacterRemoving:Connect(function(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
	end)
end

local function ToolData2(plr)
	local ToolTable = {}

	for i,v in pairs(plr.Backpack:GetChildren()) do
		table.insert(ToolTable, v.Name)
	end
	if ToolTable ~= nil then
		SaveData:SetAsync(plr.UserId, ToolTable)
	end
end

local function Group(plr)
	if plr:WaitForChild("Once").Value == false then
		if plr:IsInGroup(5406474) then
			plr:WaitForChild("Once").Value = true
			plr.leaderstats.Cash.Value += 1000
			plr:WaitForChild("XP").Value += 50
		end
	end
end

local function Save2(plr)
	local success,errormessage = pcall(function()
		KillStore:SetAsync(plr.UserId.."-kill",plr.leaderstats.Kill.Value)
		KillStore:SetAsync(plr.UserId.."-death",plr.leaderstats.Death.Value)
		KillStore:SetAsync(plr.UserId.."-cash",plr.leaderstats.Cash.Value)
		KillStore:SetAsync(plr.UserId.."-once",plr.Once.Value)
		KillStore:SetAsync(plr.UserId.."-level",plr.leaderstats.Level.Value)
		KillStore:SetAsync(plr.UserId.."-XP",plr.XP.Value)

	end)
	if success then

		print("player Data successfully saved!")
	else
		print("There was an error saving data!")
		warn(errormessage)
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	leaderstats(plr)
	Save(plr)
	ToolData1(plr)
	Group(plr)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	Save2(plr)
	ToolData2(plr)
end)
1 Like

Are you testing this in studio?

Yes I am testing this in studio!

Use game:BindToClose() to save in studio

2 Likes

DataStores won’t save in studio like so, try publishing and testing in-game

1 Like

Assuming there is nothing wrong with the code, you need to use game:BindToClose() to be able to save players’ data in studio and when the game shuts down:

game:BindToClose(function()
    for _, player in game.Players:GetPlayers() do
        Save2(player)
        ToolData2(player)
    end
end)

I’d add this at the very bottom of your script.

no datastores will work in studio

2 Likes

Not with the way he is using them

2 Likes

You can use BindToClose to detect yourself leaving in studio, but chances are it just won’t save

2 Likes

So just test in game and it will work?

1 Like

Just put this at the end of your code, and it should work properly (both in studio and actual game)

game:BindToClose(function()
    if game:GetService("RunService"):IsStudio() then
       task.wait(2)
    end
end)
1 Like

this is false information Datastores do save in studio

1 Like

To clarify, By default, they do not save in studio. However, it is possible to make them do so by enabling “Enable Studio Access to API Services” which can be found in the game settings. If you wish to learn on how to do this, Refer to this documentation: Save data | Documentation - Roblox Creator Hub

@LeanderDevForum Have you tried enabling this?


Since Datastores are an API Service, Studio can’t access them unless you permit it to do so. My assumption as to why its off by default is to prevent the risk of development on the game breaking datastores if testing something around them, seperating them allows you to reconfigure stuff without the risk of having everyone’s data lost. It’s also partially why people develop in seperate files rather than doing everything in the main one.

3 Likes

can you read, i said “like so”. thanks @Kyoobuyu_Shurai for clarifying

1 Like

You seem to not read the Title which I said in Api is on!

Must’ve missed it, sorry.

Did you try @voozy_v 's solution?

I’m assuming you missed these since you never said if they didn’t work, apologies if you did test them and they didn’t work.

There’s more information on the Documentation but these two lines seem somewhat relevant:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.