Hey! 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.
The script’s location:
The script:
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Chr)
wait(0.5)
local leaderstats = player.leaderstats
local timeAliveData = leaderstats["Time"]
local bestTimeData = leaderstats["TopTime"]
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 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)
end)
I already have a separate script that creates the leaderstats folder.
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 this is where the folder gets created.
From what I understand, let’s say player A hits player B with his sword and kills him. Then, Creator is a StringValue that has A’s name and B’s humanoid will be its parent. Is that the case?
local CreatorLeaderstats = CreatorTag.Value:WaitForChild("leaderstats", 5)
With this:
local CreatorPlayer = game.Players:WaitForChild(CreatorTag.Value)
local CreatorLeaderstats
if CreatorPlayer then
CreatorLeaderstats = CreatorPlayer:WaitForChild("leaderstats", 5)
else
print("CreatorPlayer doesn't exist.")
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Chr)
wait(0.5)
local leaderstats = player:WaitForChild("leaderstats")
local timeAliveData = leaderstats["Time"]
local bestTimeData = leaderstats["TopTime"]
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 CreatorPlayer = game.Players:WaitForChild(CreatorTag.Value)
local CreatorLeaderstats
if CreatorPlayer then
CreatorLeaderstats = CreatorPlayer:WaitForChild("leaderstats", 5)
else
print("CreatorPlayer doesn't exist.")
end
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)
end)
Your script here supposes that when a player dies, his humanoid should have a child called Creator.
So, I don’t know how it’s made. Check your other scripts, they probably have something related to this “Creator”.
Hey @Bou_Baker, none of my other scripts have something related to “Creator”, I believed just typing “Creator” had to do with the Humanoid itself but, now I realize it’s suppose to have a StringValue right?
(Am new to scripting sorry if my questions are basic have nothing to do with the line of code. .)
Ok here’s how I think the game should work:
Everybody spawns with a StringValue called Creator in their Humanoid. The Creator should has the name of the player who killed with his sword. So if A kills B, when B dies, B.Humanoid.Creator.Value should have A’s name. So, we need to implement a script that whenever A deals damage to B, The creator value on B’s humanoid should be changed to A’s name. Basically, the Creator value of B should change to whoever damaged him last.This way, if B dies, we check for his Creator value and it should have the name of the player who killed him.