Needing help on a leaderstats script

Hello, I’m trying to make a game where whenever you swing a sword, you gain skill, and the skill goes onto a leaderstats leaderboard. However, I get output errors whenever I try to make one, saying that I tried to index nil with the variable. Here’s my code.

Sword Code

task.wait(1.5)

local Character
local Player

local ScriptsFolder = script.Parent
local Tool = ScriptsFolder.Parent
local AnimationsFolder = Tool:WaitForChild("Animations")

local epicSwingAnimation = AnimationsFolder:WaitForChild("epicSwordSwing")
local epicSwingTrack

local Cooldown = false
local CooldownWait = 0.75

local SkillPoints

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
	SkillPoints = Player.PlayerSkill

	local humanoid = Character:FindFirstChild("Humanoid")

	if humanoid then
		epicSwingTrack = humanoid:LoadAnimation(epicSwingAnimation)
	end
end)


Tool.Activated:Connect(function()
	if Cooldown == false then
		Cooldown = true

		SkillPoints.Value += 5
		print("You earned 5 skill, and now have", SkillPoints.Value, "Skill Points!")
		epicSwingTrack:Play()

		wait(CooldownWait)
		Cooldown = false
	end
end)

Skill Points Code

local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerSkillDataStore = dataStores:GetDataStore("PlayerSkill")

players.PlayerAdded:Connect(function(plr)
	
	PlayerSkill = Instance.new('IntValue')
	PlayerSkill.Name = "PlayerSkill"
	PlayerSkill.Parent = plr
	
	task.wait(1)
	
	PlayerSkill.Value = 0
	
	local SkillData = playerSkillDataStore:GetAsync(plr.UserId)
	
	if SkillData then
		print("Player has data")
		PlayerSkill.Value = SkillData
		if PlayerSkill.Value >= 1 then
			print("You already have some skill, let's get some more!")
			
		else
			print("You don't have any skill, so let's get some!")
			
		end
	
	else
		print("Player has no data... womp womp")
		playerSkillDataStore:SetAsync(plr.UserId, 0)
		print("Player now has data... yipee!!!")
		print("It's time to start gaining skill!")
		while true do 
			task.wait(1)
			PlayerSkill.Value += 1
			print(PlayerSkill.Value)
		end
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local PlayerSkill = plr:WaitForChild("PlayerSkill")
	playerSkillDataStore:SetAsync(plr.UserId, PlayerSkill.Value)
end)


If you know anything about how to make a leaderstats folder to display Skill Points please reply to this post!

                                                            - rip_Zephyr (Pish85) -

Try this:

Sword Code:

task.wait(1.5)

local Character
local Player
local SkillPoints

local ScriptsFolder = script.Parent
local Tool = ScriptsFolder.Parent
local AnimationsFolder = Tool:WaitForChild("Animations")

local epicSwingAnimation = AnimationsFolder:WaitForChild("epicSwordSwing")
local epicSwingTrack

local Cooldown = false
local CooldownWait = 0.75

Tool.Equipped:Connect(function()
	Character = Tool.Parent
	Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
	SkillPoints = Player:WaitForChild("PlayerSkill") -- Wait for PlayerSkill to be available

	local humanoid = Character:FindFirstChild("Humanoid")

	if humanoid then
		epicSwingTrack = humanoid:LoadAnimation(epicSwingAnimation)
	end
end)


Tool.Activated:Connect(function()
	if Cooldown == false then
		Cooldown = true

		SkillPoints.Value += 5
		print("You earned 5 skill, and now have", SkillPoints.Value, "Skill Points!")
		epicSwingTrack:Play()

		wait(CooldownWait)
		Cooldown = false
	end
end)

Skill Points Code:

local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerSkillDataStore = dataStores:GetDataStore("PlayerSkill")

players.PlayerAdded:Connect(function(plr)
	
	local PlayerSkill = Instance.new('IntValue')
	PlayerSkill.Name = "PlayerSkill"
	PlayerSkill.Parent = plr
	
	task.wait(1)
	
	PlayerSkill.Value = 0
	
	local SkillData = playerSkillDataStore:GetAsync(plr.UserId)
	
	if SkillData then
		print("Player has data")
		PlayerSkill.Value = SkillData
		if PlayerSkill.Value >= 1 then
			print("You already have some skill, let's get some more!")
			
		else
			print("You don't have any skill, so let's get some!")
			
		end
	
	else
		print("Player has no data... womp womp")
		playerSkillDataStore:SetAsync(plr.UserId, 0)
		print("Player now has data... yipee!!!")
		print("It's time to start gaining skill!")
		while true do 
			task.wait(1)
			PlayerSkill.Value += 1
			print(PlayerSkill.Value)
		end
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local PlayerSkill = plr:WaitForChild("PlayerSkill")
	playerSkillDataStore:SetAsync(plr.UserId, PlayerSkill.Value)
end)

Sorry, but did you read my post?

leaderstats and the associated player stats leaderboard requires a specific setup:

The main point being that you will need to create the leaderstats folder as part of your players.PlayerAdded:Connect(function(plr):

local leaderstats = Instance.new(Folder)
leaderstats.Name = leaderstats
leaderstats.Parent = player

local PlayerSkill = Instance.new('IntValue')
PlayerSkill.Name = "PlayerSkill"
PlayerSkill.Parent = leaderstats 

With regards to the error, what line does it error on and in which script. Without this information, people have to trawl through your code trying to work out where the error us.
As they say “help us to help you”

1 Like

I’m really sorry but I forgot to mention I posted a second post because I thought this one was dead and the other one found the solution sorry for wasting your time

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.