I currently working on a game and I have a DataStore script that creates the leaderstats saves them.
In the middle of the script there is a piece of code that is supposed to run when the player kills another player they get their stats.
I edited the code to my preference, unfortunately, the code does not work.
(Credits to @JackscarIitt for the original code).
The script:
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
local function CreateStat(name, class, value, parent)
local stat = Instance.new(class)
stat.Name = name
stat.Value = value
stat.Parent = parent
return stat
end
local function GetKey(player)
return player.UserId .. "-"
end
local function SaveData(player)
local key = GetKey(player)
local leaderstats = player.leaderstats
local timeAliveData = leaderstats["Time"].Value
local bestTimeData = leaderstats["TopTime"].Value
local kills = leaderstats["Kills"].Value
local success, result = pcall(function()
DataStore:SetAsync(key .. "Time", timeAliveData)
DataStore:SetAsync(key .. "TopTime", bestTimeData)
DataStore:SetAsync(key .. "Kills", kills)
end)
if not success then
warn(result)
end
end
game.Players.PlayerAdded:Connect(function(player)
local key = GetKey(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local timeAlive = CreateStat("Time", "IntValue", 0, leaderstats)
local bestTime = CreateStat("TopTime", "IntValue", 0, leaderstats)
local bestTime = CreateStat("Kills", "IntValue", 0, leaderstats)
local timeAliveData, bestTimeData, kills
local success, result = pcall(function()
timeAliveData = DataStore:GetAsync(key .. "Time")
bestTimeData = DataStore:GetAsync(key .. "TopTime")
kills = DataStore:GetAsync(key .. "Kills")
game.Players.PlayerAdded:Connect(function(player) --Code that does not work
print("Player Added")
player.CharacterAdded:Connect(function(Chr)
print("Character added")
local Humanoid = Chr:WaitForChild("Humanoid")
print(player.Name.." has died!")
Humanoid.Died:Connect(function()
local CreatorTag = Humanoid:FindFirstChild("creator")
if CreatorTag then
local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
timeAliveData += Leaderstats["Time Alive"].Value
bestTimeData = timeAliveData
Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0
print("Stole time!")
else
warn("Something else happened?")
end
end)
end)
end)
end) -- Ends here
if success then
timeAlive.Value = timeAliveData or 0
bestTime.Value = bestTimeData or 0
bestTime.Value = kills or 0
else
warn(result)
print ("didnt save")
end
end)
game.Players.PlayerRemoving:Connect(SaveData)
if not RunService:IsStudio() then
game:BindToClose(function()
if #game.Players:GetPlayers() <= 1 then return end
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end)
end
This is the code found in the middle of the script that won’t work.
game.Players.PlayerAdded:Connect(function(player) --Code that does not work
print("Player Added")
player.CharacterAdded:Connect(function(Chr)
print("Character added")
local Humanoid = Chr:WaitForChild("Humanoid")
print(player.Name.." has died!")
Humanoid.Died:Connect(function()
local CreatorTag = Humanoid:FindFirstChild("creator")
if CreatorTag then
local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
timeAliveData += Leaderstats["Time Alive"].Value
bestTimeData = timeAliveData
Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0
print("Stole time!")
else
warn("Something else happened?")
end
end)
end)
end)
end) -- Ends here
Alright one issue: You’re encasing 2 PlayerAdded events inside of each other which is why the second script isn’t working since the first one has already fired, and went ahead to create those stats & GetAsync functions (Also why are you encasing it all in a pcall? It’s not necessary
You don’t need to add any more PlayerAdded events, 1 is fine
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
local function CreateStat(name, class, value, parent)
local stat = Instance.new(class)
stat.Name = name
stat.Value = value
stat.Parent = parent
return stat
end
local function GetKey(player)
return player.UserId .. "-"
end
local function SaveData(player)
local key = GetKey(player)
local leaderstats = player.leaderstats
local timeAliveData = leaderstats["Time"].Value
local bestTimeData = leaderstats["TopTime"].Value
local kills = leaderstats["Kills"].Value
local success, result = pcall(function()
DataStore:SetAsync(key .. "Time", timeAliveData)
DataStore:SetAsync(key .. "TopTime", bestTimeData)
DataStore:SetAsync(key .. "Kills", kills)
end)
if not success then
warn(result)
end
end
game.Players.PlayerAdded:Connect(function(player)
local key = GetKey(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local timeAlive = CreateStat("Time", "IntValue", 0, leaderstats)
local bestTime = CreateStat("TopTime", "IntValue", 0, leaderstats)
local bestTime = CreateStat("Kills", "IntValue", 0, leaderstats)
local timeAliveData, bestTimeData, kills
local success, result = pcall(function()
timeAliveData = DataStore:GetAsync(key .. "Time")
bestTimeData = DataStore:GetAsync(key .. "TopTime")
kills = DataStore:GetAsync(key .. "Kills")
end)
if success then
timeAlive.Value = timeAliveData or 0
bestTime.Value = bestTimeData or 0
bestTime.Value = kills or 0
else
warn(result)
print ("didnt save")
end
print("Player Added")
player.CharacterAdded:Connect(function(Chr)
print("Character added")
local Humanoid = Chr:WaitForChild("Humanoid")
print(player.Name.." has died!")
Humanoid.Died:Connect(function()
local CreatorTag = Humanoid:FindFirstChild("creator")
if CreatorTag then
local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
timeAliveData += Leaderstats["Time Alive"].Value
bestTimeData = timeAliveData
Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0
print("Stole time!")
else
warn("Something else happened?")
end
end)
end)
end)
game.Players.PlayerRemoving:Connect(SaveData)
if not RunService:IsStudio() then
game:BindToClose(function()
if #game.Players:GetPlayers() <= 1 then return end
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end)
end
@JackscarIitt, I am aware that there are prints in the script but, when I test them in a 2 player local server the prints don’t appear at all in the output at all.
Are you checking the prints on the server window? It should be able to print just fine if you check it from there and everything’s working fine? If not, try adding a print before you even start your script?
Oh this is because the “Died” print is inside the CharacterAdded event
So breaking it down to these lines here:
player.CharacterAdded:Connect(function(Chr)
print("Character added")
local Humanoid = Chr:WaitForChild("Humanoid")
Humanoid.Died:Connect(function()
print(player.Name.." has died!")
local CreatorTag = Humanoid:FindFirstChild("creator")
print(CreatorTag)
if CreatorTag then
print("Found a tag")
local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
timeAliveData += Leaderstats["Time Alive"].Value
bestTimeData = timeAliveData
Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0
print("Stole time!")
else
warn("Something else happened?")
end
end)
end)
Why is it that Humanoid objects are the most struggling thing to get? Are you getting any infinite yield warnings for attempting to find the Humanoid variable?
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
local function CreateStat(name, class, value, parent)
local stat = Instance.new(class)
stat.Name = name
stat.Value = value
stat.Parent = parent
return stat
end
local function GetKey(player)
return player.UserId .. "-"
end
local function SaveData(player)
local key = GetKey(player)
local leaderstats = player.leaderstats
local timeAliveData = leaderstats["Time"].Value
local bestTimeData = leaderstats["TopTime"].Value
local kills = leaderstats["Kills"].Value
local success, result = pcall(function()
DataStore:SetAsync(key .. "Time", timeAliveData)
DataStore:SetAsync(key .. "TopTime", bestTimeData)
DataStore:SetAsync(key .. "Kills", kills)
end)
if not success then
warn(result)
end
end
game.Players.PlayerAdded:Connect(function(player)
local key = GetKey(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local timeAlive = CreateStat("Time", "IntValue", 0, leaderstats)
local bestTime = CreateStat("TopTime", "IntValue", 0, leaderstats)
local bestTime = CreateStat("Kills", "IntValue", 0, leaderstats)
local timeAliveData, bestTimeData, kills
local success, result = pcall(function()
timeAliveData = DataStore:GetAsync(key .. "Time")
bestTimeData = DataStore:GetAsync(key .. "TopTime")
kills = DataStore:GetAsync(key .. "Kills")
local Chr = player.Character or player.CharacterAdded:Wait()
print("Character exists")
repeat wait() until Chr:FindFirstChild("Humanoid")
local Humanoid = Chr.Humanoid
Humanoid.Died:Connect(function()
print(player.Name.." has died!")
local CreatorTag = Humanoid:FindFirstChild("creator")
if CreatorTag then
local Leaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
timeAliveData += Leaderstats["Time Alive"].Value
bestTimeData = timeAliveData
Leaderstats["Time Alive"].Value = 0 --Resetting the Target's time to 0
print("Stole time!")
else
warn("Something else happened?")
end
end)
end) -- Ends here
if success then
timeAlive.Value = timeAliveData or 0
bestTime.Value = bestTimeData or 0
bestTime.Value = kills or 0
else
warn(result)
print ("didnt save")
end
end)
game.Players.PlayerRemoving:Connect(SaveData)
if not RunService:IsStudio() then
game:BindToClose(function()
if #game.Players:GetPlayers() <= 1 then return end
for _, player in pairs(game.Players:GetPlayers()) do
SaveData(player)
end
end)
end