Problem with take leaderstats scripts, help is appreciated!

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:
image


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.


More detailed example of what am trying to achieve:
Screen capture - f8ce0224e1a7e9a0028da277b4811a81 - Gyazo

I’ve been in need of help with this script for a while now, help is really, really appreciated. :happy1:

I had another post with this exact same scripting problem, not that many people responded so, this is my third post… Please help!

you need to create a leaderstats folder in the player

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Parent = Player
	Leaderstats.Name = "leaderstats"
end)

Hey @LuiOG, I already have a separate script that does this, sorry for not informing about this in the post. (I will edit it right now)

ah alright, try

local leaderstats = player:WaitForChild("leaderstats")

Hey @LuiOG, I tried this. Unfortunately it didn’t work. :cry:

hm alright, can i see the script where you create the leaderstats folder?

1 Like

Sure thing, @LuiOG.

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.

@LuiOG, question, is “Creator” supposed to be capitalized or lowercased?

local CreatorTag = Humanoid:FindFirstChild("Creator")

What’s this Creator under Humanoid? is it a StringValue?

Hey @Bou_Baker, There is no Creator under Humanoid.

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?

1 Like

Hey @Bou_Baker, yes, that’s basically what’s supposed to happen but, how exactly would the StringValue be parented to the humanoid? :thinking:

Try replacing this line:

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

Hey @Bou_Baker, is this how it should be written?

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)

@Bou_Baker, I tried your script but, it didn’t work :cry:. Wouldn’t the script have to create a stringvalue for a the humanoid though? :thinking:

If so, how would I be able to do that?

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”.

1 Like

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. :sweat_smile:.)

Would I just have to create a script that automatically gives a humanoid it’s StringValue?

If that’s the case then how can I do that?

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.

1 Like

Hey @Bou_Baker, I see where you’re going with this. Where should I start, how can I make every humanoid start with a StringValue?