Why is my take leaderstats script not working? Please help!

Hey there!

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


The script’s location:
image


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)

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:

Can you actually check if there’s “leaderstats” inside the player? If there is try doing :WaitForChild(‘leaderstats’) instead of “.leaderstats”

1 Like

Hey @BANSA168, i’ll try that now.

Hey @BANSA168, I tried your code. The output did only printed “Character Exists” but it never did what the script was intended to do. :sad:

did you already create the leaderstat folder?

@Dorthsky, Yes, I have already created a leaderstats folder.

I have included a Gyazo link to the post.

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)

Hey @Dorthsky, I tried your code. It printed “Character Exists” only, it never printed the other prints. :sad:

Can you send me the code that created the leaderstat? Otherwise, make sure you’re going to the right directory.

Sure thing, @Dorthsky.

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.

These are all in serverscriptstorage, right? i’m trying to repro it.

1 Like

Sorry for the late response, @Dorthsky. The script is parented to a tool in the StarterPack.

Both of them? or the one that inserts the leaderstat.

1 Like

The DataStore script, which creates the folder, is located in ServerScriptService. The Take Time one is parented to a tool in the StarterPack.