Leaderstats not being vaild

This script makes it so if you Hit the part that it would open up a certain GUI depending on how much of a certain leaderstats you have (Coins).

But I get this error.

Error21

local part = script.Parent

part.Touched:Connect(function(hit)
	local plr = hit.Parent:FindFirstChild('Humanoid')
	if hit.Parent:FindFirstChild('Humanoid')and plr.leaderstats.Coins.Value <= 100 then
		plr.PlayerGui.HaveCoins.Visible = true
	elseif hit.Parent:FindFirstChild('Humanoid')and plr.leaderstats.Coins.Value >= 100 then
		plr.PlayerGui.NotHaveCoins.Visible = true
	end
end)

Where are your leaderstats located - in the player Model or within the Players service? With the assumption that your leaderstats are found within the Player Object (of Players service) then use this function:

local player = Players:GetPlayerFromCharacter(part.Parent)

1 Like

You’re trying to retrieve leaderstats from your humanoid. That isn’t how it works. Since it’s located in the player, you must get the player from the character model. So, do this:
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

2 Likes

Ok at @ItzMeZeus_IGotHacked and @kboy100

I did you edits and not I got this error.

New Script

local part = script.Parent

part.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if hit.Parent:FindFirstChild('Humanoid')and plr.leaderstats.Coins.Value <= 100 then
		plr.PlayerGui.HaveCoins.Visible = true
	elseif hit.Parent:FindFirstChild('Humanoid')and plr.leaderstats.Coins.Value >= 100 then
		plr.PlayerGui.NotHaveCoins.Visible = true
	end
end)

You spelt HaveCoins, not NotHaveCoins.

Where in the script? Like in which line?

Can’t you notice it? It’s literally at around middle of the code.

If I change its the same error but now with is saying HaveCoins and not NotHaveCoins

When you create the leaderstats folder, where are you parenting it to?

Please also provide some code.

The Player of in this case Madden21fan (Me)

Maybe you forgot to name the ScreenGui, if there is one

local part = script.Parent

part.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not plr then				return				end
	
	local Screen = plr.PlayerGui[""] -- ScreenGui name
	if plr.leaderstats.Coins.Value <= 100 then
		Screen.HaveCoins.Visible = true
	elseif plr.leaderstats.Coins.Value >= 100 then
		Screen.NotHaveCoins.Visible = true
	end
end)

There is one. It is there I spent like 30 min on it…

you do know that if coins is 100, then both if statements will run

I don’t know, this is the script he gave :woman_shrugging:

It should be >=100 and <100

You should have helped me correct my mistake.

So this script works.But it gives up the same message no matter how many coins you have.

local part = script.Parent

part.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not plr then				return				end

	local Screen = plr.PlayerGui["TeleporterGUI"] -- ScreenGui name
	if plr.leaderstats.Coins.Value < 100 then
		Screen.NotHaveCoins.Visible = true
	elseif plr.leaderstats.Coins.Value >= 100 then
		Screen.HaveCoins.Visible = true
	end
end)

P.S. So I have like a 1,400 coins and I change it to 99 and I wonder if it didn’t register.

Ok with a little bit of print magic apparently it didn’t register.