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. I asked this on a different post before but the user I03v (JellyM015) decided to copy and paste my code and told me to try it out and didnt even have anything to do with leaderstats. Here’s my code:
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 System 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 are the user I mentioned above please don’t waste my time again
i know how to make standard leaderstats im just wondering how can i transfer the variable SkillPoints into the other script that handles leaderstats (sorry if this wasnt in desc)
local players = game:GetService("Players")
local dataStores = game:GetService("DataStoreService")
local playerSkillDataStore = dataStores:GetDataStore("PlayerSkill")
players.PlayerAdded:Connect(function(plr)
leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
PlayerSkill = Instance.new('IntValue')
PlayerSkill.Name = "PlayerSkill"
PlayerSkill.Parent = leaderstats
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)