Script can't find leaderstats value inside player

In my game you get payed every 5 minutes or so and with the currency you can purchase hats.

The hat giver works just fine, but whenever I make it charge you for the hat it breaks. The error says: “Leaderstats is not a valid member of Players.playername” and I don’t really know how to fix. Here’s the script.

--vari
fortnite = game.Players.LocalPlayer.leaderstats.Money.Value
--end

debounce = true



function onTouched(hit)
	if (hit.Character:FindFirstChild("Humanoid") ~= nil and debounce == true) then
		debounce = false
	end
	if fortnite >= 25 then
		fortnite = fortnite - 25
		local h = Instance.new("Hat")
		local p = Instance.new("Part")
		h.Name = script.Parent.Parent.Name
		p.Parent = h
		p.Position = hit.Character:FindFirstChild("Head").Position
		p.Name = "Handle" 
		p.formFactor = 0
		p.Size = Vector3.new(0,-0.25,0) 
		p.BottomSurface = 0 
		p.TopSurface = 0 
		p.Locked = true 
		script.Parent.Mesh:clone().Parent = p
		h.Parent = hit.Character
		h.AttachmentPos = Vector3.new(0,-0.25,0)
		hit.Character.Hair.Transparency = 1
	end
	wait(5)
	debounce = true


	script.Parent.ClickDetector.MouseClick:Connect(onTouched)
end

Help is appreciated

1 Like
fortnite = game.Players.LocalPlayer.leaderstats.Money.Value

Remove .Value from this and add .Value to each other reference to fortnite.

1 Like

You’re trying to reference the leaderstats folder before it’s created. Try this:

fortnite = game.Players.LocalPlayer:WaitForChild(“leaderstats”).Money.Value

hm, it just so seems it unfortunately no worki.

Is this script inside a LocalScript at all? Also as @Forummer, change the line to:

local fortnite = game.Players.LocalPlayer.leaderstats.Money

And whenever you need the value, just do fortnite.Value. Though I’m not sure why you are naming your variables like this, they should be clear and precise on what they’re meant to hold, thanks.

Also the WaitForChild as Louis mentioned, replication errors are common. If it’s not a LocalScript, it won’t most likely run if it’s inside the Player, or it will not find <Players>.LocalPlayer since it is nil in the server.

Is this working?

--vari
local fortnite = game.Players.LocalPlayer.leaderstats.Money
--end

local debounce = true



function onTouched(hit)
	if (hit.Character:FindFirstChild("Humanoid") ~= nil and debounce == true) then
		debounce = false
	end
	if fortnite.Value >= 25 then
		fortnite.Value = fortnite.Value - 25
		local h = Instance.new("Hat")
		local p = Instance.new("Part")
		h.Name = script.Parent.Parent.Name
		p.Parent = h
		p.Position = hit.Character:FindFirstChild("Head").Position
		p.Name = "Handle" 
		p.formFactor = 0
		p.Size = Vector3.new(0,-0.25,0) 
		p.BottomSurface = 0 
		p.TopSurface = 0 
		p.Locked = true 
		script.Parent.Mesh:clone().Parent = p
		h.Parent = hit.Character
		h.AttachmentPos = Vector3.new(0,-0.25,0)
		hit.Character.Hair.Transparency = 1
	end
	wait(5)
	debounce = true


	script.Parent.ClickDetector.MouseClick:Connect(onTouched)
end
1 Like

script is inside server script (also variable is only named fortnite as a joke)

1 Like

As @txcIove said, you’re trying to reference leaderstats before it’s added to the player. Also, you’re assigning your fortnite variable to the value of what the value’s value is at the time you assign it. If you do it like that, it won’t update with the value.

Also, it seems that you added the listener inside of the function, which means it won’t listen unless the function is ran at least once. Simple fix, just move the listener outside of the function body.

--vari
local fortnite = game.Players.LocalPlayer:WaitForChild("leaderstats").Money
--end

local debounce = true



function onTouched(hit)
	if (hit.Character:FindFirstChild("Humanoid") ~= nil and debounce == true) then
		debounce = false
	end
	if fortnite.Value >= 25 then
		fortnite.Value = fortnite.Value - 25
		local h = Instance.new("Hat")
		local p = Instance.new("Part")
		h.Name = script.Parent.Parent.Name
		p.Parent = h
		p.Position = hit.Character:FindFirstChild("Head").Position
		p.Name = "Handle" 
		p.formFactor = 0
		p.Size = Vector3.new(0,-0.25,0) 
		p.BottomSurface = 0 
		p.TopSurface = 0 
		p.Locked = true 
		script.Parent.Mesh:clone().Parent = p
		h.Parent = hit.Character
		h.AttachmentPos = Vector3.new(0,-0.25,0)
		hit.Character.Hair.Transparency = 1
	end
	wait(5)
	debounce = true
end

script.Parent:WaitForChild("ClickDetector").MouseClick:Connect(onTouched)

it unfortunately seems the script no worki