Datastore issues

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local killstore = DSS:GetDataStore("kills")
local deathstore = DSS:GetDataStore("kills")

Why are you calling for the same DataStore “kills” for two globals?

Instead, make one DataStore for storing the Deaths, and one DataStore for the kills, which is GetDataStore("kills").

Note:
Make sure you’ve enabled also API Services and HTTP Requests.

i already fixed that

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local killstore = DSS:GetDataStore("kills")
local deathstore = DSS:GetDataStore("deathstore")

game.Players.PlayerAdded:Connect(function(player)
	-- Checks if player is KO'd, please ignore --
	local kod = Instance.new("BoolValue")
	kod.Name = "KO"
	kod.Parent = player
	--------------------------------------------------------

	-- Checks if player is being carried, please ignore --
	local carried = Instance.new("BoolValue")
	carried.Name = "Carried"
	carried.Parent = player
	--------------------------------------------------------

	-- Checks if player is ragdolled, please ignore --
	local ragdoll = Instance.new("BoolValue")
	ragdoll.Name = "Ragdoll"
	ragdoll.Parent = player
	--------------------------------------------------------

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

	local kills = Instance.new("NumberValue",leaderstats)
	kills.Name = "kills"
	local kill
	local success, notSuccess = pcall(function()
		kill = killstore:GetAsync(player.UserId)
		if kill == nil then kill = 0 end
	end)
	if success then
		kills.Value = kill
	end

	local deaths = Instance.new("NumberValue",leaderstats)
	deaths.Name = "deaths"
	local death
	local success, notSuccess = pcall(function()
		death = deathstore:GetAsync(player.UserId)
		if death == nil then death = 0 end
	end)
	if success then
		deaths.Value = death
	end

	player.CharacterAdded:connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")
		humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local success, notSuccess = pcall(function()
				deathstore:SetAsync(player.UserId,player.leaderstats.deaths.Value)
			end)
			local tag = humanoid:FindFirstChild("creator")
			local killer = tag.Value
			if tag and killer then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("kills").Value + 1
				local success, notSuccess = pcall(function()
					killstore:SetAsync(killer.UserId,killer.leaderstats.kills.Value)
				end)
			end
		end)
	end)
end)

and it still just doesn’t work for some reason

1 Like

I tried doing the same code that i have but with only one of the datastores and it still doesn’t work at all
does anyone have an example of a script that acesses a datastore and works?

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local killstore = DSS:GetDataStore("kills")

game.Players.PlayerAdded:Connect(function(player)
	-- Checks if player is KO'd, please ignore --
	local kod = Instance.new("BoolValue")
	kod.Name = "KO"
	kod.Parent = player
	--------------------------------------------------------

	-- Checks if player is being carried, please ignore --
	local carried = Instance.new("BoolValue")
	carried.Name = "Carried"
	carried.Parent = player
	--------------------------------------------------------

	-- Checks if player is ragdolled, please ignore --
	local ragdoll = Instance.new("BoolValue")
	ragdoll.Name = "Ragdoll"
	ragdoll.Parent = player
	--------------------------------------------------------

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

	local kills = Instance.new("NumberValue",leaderstats)
	kills.Name = "kills"
	local kill
	local success, notSuccess = pcall(function()
		kill = killstore:GetAsync(player.UserId)
		if kill == nil then kill = 0 end
	end)
	if success then
		kills.Value = kill
	end

	player.CharacterAdded:connect(function(character)
	local humanoid = character:FindFirstChild("Humanoid")
		local tag = humanoid:FindFirstChild("creator")
		local killer = tag.Value
		if tag and killer then
			killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("kills").Value + 1
			local success, notSuccess = pcall(function()
				killstore:SetAsync(killer.UserId,killer.leaderstats.kills.Value)
			end)
		end
	end)
end)

Have you got API Services on in game?

yes I do indeed have api services enabled ingame and in studio (fancy words for char limit)

Print something when it saves succesfully or load succesfully. If something doesn’t print, then thats the problem, and we will have less things to find out why it doesn’t work.

would now be a good time to say this is my first time using pcalls?

1 Like
local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local killstore = DSS:GetDataStore("kills")
local deathstore = DSS:GetDataStore("deathstore")

game.Players.PlayerAdded:Connect(function(player)
	-- Checks if player is KO'd, please ignore --
	local kod = Instance.new("BoolValue")
	kod.Name = "KO"
	kod.Parent = player
	--------------------------------------------------------

	-- Checks if player is being carried, please ignore --
	local carried = Instance.new("BoolValue")
	carried.Name = "Carried"
	carried.Parent = player
	--------------------------------------------------------

	-- Checks if player is ragdolled, please ignore --
	local ragdoll = Instance.new("BoolValue")
	ragdoll.Name = "Ragdoll"
	ragdoll.Parent = player
	--------------------------------------------------------

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

	local kills = Instance.new("NumberValue",leaderstats)
	kills.Name = "kills"
	local kill
	local success, notSuccess = pcall(function()
		kill = killstore:GetAsync(player.UserId)
		if kill == nil then print("No savings") kill = 0 end
	end)
	if success then
		print("Loaded")
		kills.Value = kill
	end

	local deaths = Instance.new("NumberValue",leaderstats)
	deaths.Name = "deaths"
	local death
	local success, notSuccess = pcall(function()
		death = deathstore:GetAsync(player.UserId)
		if death == nil then print("No savings") death = 0 end
	end)
	if success then
		print("Loaded")
		deaths.Value = death
	end

	player.CharacterAdded:connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")
		humanoid.Died:Connect(function(died)
			deaths.Value = deaths.Value + 1
			local success, notSuccess = pcall(function()
				deathstore:SetAsync(player.UserId,player.leaderstats.deaths.Value)
			end)
			if success then
				print("Saved")
			end
			local tag = humanoid:FindFirstChild("creator")
			local killer = tag.Value
			if tag and killer then
				killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("kills").Value + 1
				local success, notSuccess = pcall(function()
					killstore:SetAsync(killer.UserId,killer.leaderstats.kills.Value)
				end)
				if success then
					print("Saved")
				end
			end
		end)
	end)
end)

image

what about saved prints? or they didn’t print anything?

image

Oh wait, i did mistake, lemme edit message with print’s, try edited message.

does it matter if i test in studio or in roblox (aka will the datastores be wierd because player1and player2 have wierd uids?)

Actually idk, try both with 2 players and with 1, play test.

same output in studio and roblox and also the edit didn’t work

yeah i think the problem is it never saves for some reason?

Maybe try to change deaths.Value to player.leaderstats.deaths.Value, i mean:

player.leaderstats.deaths.Value = player.leaderstats.deaths.Value + 1

and you can shorten it:

player.leaderstats.deaths.Value += 1

But also, do they showing in Leaderstats Tab? Can you see how many kills/deaths you got? Or it’s just don’t save em.

the kills/deaths work in leaderboard but they don’t save

You can create a DataStore Save Function via the leaderboard being changed or just the leaderboard in general.

Please refer to these posts: