Hello, I’m making a fighting game where there are two leaderboard stats, Kills and All time Kills. I want the alltime kills to save with a datastore but for some reason it isnt working. I don’t get ANY output errors so I don’t know what’s wrong. Here is my code:
local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerAllTimeKillsDataStore = dataStores:GetDataStore("All Time Kills")
players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
local playerKills = Instance.new('NumberValue')
playerKills.Name = "Kills"
playerKills.Parent = leaderstats
local playerAllTimeKills = Instance.new('NumberValue')
playerAllTimeKills.Name = "All Time Kills"
playerAllTimeKills.Parent = leaderstats
task.wait(1)
local playerAllTimeKillsData = playerAllTimeKillsDataStore:GetAsync(plr.UserId)
if playerAllTimeKillsData then
print("Player has data.")
playerAllTimeKills.Value = playerAllTimeKillsData
if playerAllTimeKills.Value >= 1 then
print("You already have some kills.")
end
else
print("Player has no data.")
playerAllTimeKillsDataStore:SetAsync(plr.UserId, 0)
print("Player now has data.")
end
end)
players.PlayerRemoving:Connect(function(plr)
local leaderboardThing = plr:WaitForChild("leaderstats")
local playerAllTimeKills = leaderboardThing:WaitForChild("All Time Kills")
playerAllTimeKillsDataStore:SetAsync(plr.UserId, playerAllTimeKills.Value)
end)
– I have studio access to api services enabled
– I only want AllTimeKills to save as a datastore, not kills.
If anyone knows how to make my code actually save AllTimeKills Data, please reply down below!
I don’t know a lot about datastore, actually it’s my biggest nightmare. But i think that it’s not working because you created the folders inside the PlayerAdded function. Maybe creating the folders in a separate script and accessing it from that one will work. But i could be wrong, i barely know how datastoring works.
local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerAllTimeKillsDataStore = dataStores:GetDataStore("All Time Kills")
local leaderstats, allTimeKills = nil, nil
local function savePlayerData(plr)
leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then allTimeKills = leaderstats:FindFirstChild("All Time Kills")
if allTimeKills then
local sus, err = pcall(function()
playerAllTimeKillsDataStore:SetAsync(plr.UserId, allTimeKills.Value)
end) if not sus then warn("Save Data Error: " .. err) end
end
end
end
players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local playerKills = Instance.new("NumberValue")
playerKills.Name = "Kills"
playerKills.Parent = leaderstats
local playerAllTimeKills = Instance.new("NumberValue")
playerAllTimeKills.Name = "All Time Kills"
playerAllTimeKills.Parent = leaderstats
local data = 0
local sus, err = pcall(function()
data = playerAllTimeKillsDataStore:GetAsync(plr.UserId) or 0
end) if not sus then warn("Load Data Error: " .. err) end
playerAllTimeKills.Value = data
end)
players.PlayerRemoving:Connect(savePlayerData)
game:BindToClose(function()
for _, plr in pairs(players:GetPlayers()) do
savePlayerData(plr)
end
end)
SavePlayerData() is also link with BindToClose, for shut downs. So it’s best for this version to use FindFirstChild and quick check. Loads data on join, saves data on exit along with a shut down trap.
Added load/save data error warnings … helps if data error to not disable the script and debugging.
Problem 1: It breaks my punch code (The animation and the damage dealing), if I use my original non-functional script the punch script works, maybe there’s an interference of some sort?
Problem 2: Due to problem 1, I am unable to gain kills so I can test whether this code works or not.
Sorry you’re having problems with it … they sure were a headache for me for also.
Maybe it would be best to wait on that. I left you a link to an editable example.
Getting a solid datasave working for me was a priority, never did get much help.
Good luck with your game.