Unable to display CashValue to Leaderstats

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    The code is sopposed to a mob spawned where it keep spawning NPCs and respawning when it is less than 10, when you destroy one you earn cash. I can’t seem to get the leadstats which is under players

The first method I tried ended with and error of “(attempt to index nil with ‘FindFirstChild’)”. I tried looking at the other forums but no luck there. The second method I tried putting a yeild so that they could load but it didn’t work

First Script:

local character = game.Players
local player = game.Players:GetPlayerFromCharacter(character)
local ServerStorage = game:GetService(“ServerStorage”)
local leaderstats = player:FindFirstChild(“leaderstats”)

local MOB_RIG = ServerStorage:FindFirstChild(“Rig”)
local MAX_AMOUNT = 10
local TEST_FOLDER = workspace.TestFolder
local SPAWN_PART = workspace.MobSpawn
local CashAmount = MOB_RIG.CashAmount

local function SpawnMob()
local clone = MOB_RIG:Clone()
local cloneHumanoid:Humanoid = clone:FindFirstChild(“Humanoid”)
local cloneHumanoidRootPart = clone:FindFirstChild(“HumanoidRootPart”)

cloneHumanoidRootPart.Position = SPAWN_PART.Position
clone.Parent = TEST_FOLDER

cloneHumanoid.Died:Once(function()
SpawnMob()
CashAmount.Cash.Value += 100
end)
end

for _ = 1, MAX_AMOUNT do
SpawnMob()
end

Second Script:

local players = game.Players
local leaderStats = players:WaitForChild(“leaderstats”,5)
local cashAmount = 100
local ServerStorage = game:GetService(“ServerStorage”)

local MOB_RIG = ServerStorage:FindFirstChild(“Rig”)
local MAX_AMOUNT = 10
local TEST_FOLDER = workspace.TestFolder
local SPAWN_PART = workspace.MobSpawn

local function SpawnMob()
local clone = MOB_RIG:Clone()
local cloneHumanoid : Humanoid = clone:FindFirstChild(“Humanoid”)
local cloneHumanoidRootPart = clone:FindFirstChild(“HumanoidRootPart”)

cloneHumanoidRootPart.Position = SPAWN_PART.Position
clone.Parent = TEST_FOLDER

cloneHumanoid.Died:Once(function()
SpawnMob()
cashAmount = leaderStats:WaitForChild(“Cash”, 5).Value
end)
end

for _ = 1, MAX_AMOUNT do
SpawnMob()
end

2 Likes

You have to manually create a folder called “leaderstats” on a server script.

-- in a server script in ServerScriptService
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
end))
1 Like

I’m unsure of this occurrence but when I made the leaderstats in the same script it doesn’t show up anymore, and the script is in the ServerScriptService, if I wrote it on a seperate script then it works.

Nevermind, I just made a minor mistake.

you put this in the wrong section

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