I have a script (Credits to @JackscarIitt for a few lines). The script is parented to a sword in the StarterPack, it’s suppose to take the player’s stats but, it won’t work.
I may just have coded the script wrong but, I’m not sure if the script is suppose to fire and event or if it belongs in ServerScriptService instead.
I did recive this error in the output, perhaps this is the problem. How to fix it?
The script’s location:
The script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = player.leaderstats
local timeAliveData = leaderstats["Time"]
local bestTimeData = leaderstats["TopTime"]
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 CreatorLeaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
timeAliveData += CreatorLeaderstats["Time"].Value
bestTimeData = timeAliveData
CreatorLeaderstats["Time"].Value = 0 --Resetting the Target's time to 0
print("Stole time!")
else
warn("Something else happened?")
end
end)
end)
Try adding a wait at the start of the player added function so it has time for the folder to be made. (you might be safe with a smaller number but I just set it to that.)
game.Players.PlayerAdded:Connect(function(player)
wait(0.5)--here
local leaderstats = player.leaderstats
local timeAliveData = leaderstats["Time"]
local bestTimeData = leaderstats["TopTime"]
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 CreatorLeaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
timeAliveData += CreatorLeaderstats["Time"].Value
bestTimeData = timeAliveData
CreatorLeaderstats["Time"].Value = 0 --Resetting the Target's time to 0
print("Stole time!")
else
warn("Something else happened?")
end
end)
end)
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
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
It’s a DataStore script but, it creates the folder.