Datastore not working with no errors

so when I tried to do a basic datasave it all of a sudden didnt work/ never did work for some reason I get no errors or anything it doesnt save my data this has nothing to do with the way i changed my value because i did it the normal way in output anyways heres my script please help me out (Also it all didnt format for some reason)

`local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“DataStore”)
game.Players.PlayerAdded:Connect(function(Plr)

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

local Kills = Instance.new("IntValue", leaderstats)
Kills.Name = "Kills"

local data
local success, errormsg = pcall(function()
data = DataStore:GetAsync(Plr.UserId)
end)
if success then
	Kills.Value = data
	print("Worked")
else
	print("Error loading data")
	warn(errormsg)
end

end)

game.Players.PlayerRemoving:Connect(function(Plr)

local success, errormsg = pcall(function()
	DataStore:SetAsync(Plr.UserId, Plr.leaderstats.Kills.Value)
end)
if success then
	print("Saved Data")
else
	print("error")
	warn(errormsg)
end

end)`

1 Like

How is the kills value being changed? Is it on the client or server.

2 Likes

the value is being changed by server

1 Like

Maybe add some prints around to check the data when its being saved, and when its being loaded. If any of those show 0, then its not getting the data correctly.
For example,

local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“DataStore”)
game.Players.PlayerAdded:Connect(function(Plr)

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

local Kills = Instance.new("IntValue", leaderstats)
Kills.Name = "Kills"

local data
local success, errormsg = pcall(function()
data = DataStore:GetAsync(Plr.UserId)
print(data)
end)
if success then
	print(data)
	Kills.Value = data
	print("Worked")
else
	print("Error loading data")
	warn(errormsg)
end
end)

game.Players.PlayerRemoving:Connect(function(Plr)

local success, errormsg = pcall(function()
	print(Plr.leaderstats.Kills.Value)
	DataStore:SetAsync(Plr.UserId, Plr.leaderstats.Kills.Value)
end)
if success then
	print("Saved Data")
else
	print("error")
	warn(errormsg)
end
end)
1 Like

I found out the issue and i gave no context that could lead to it but thanks for helping so basically i created my datastore before turning on api services so i made a new datastore and it worked

2 Likes

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