Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index nil with 'leaderstats' I had this issue before how do I fix it?


script.Parent.Touched:Connect(function(hit)
	local player = game:GetService("Players")

	if hit then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player.leaderstats.Coin.Value >= script.Parent.Requirement.Value then
			player.leaderstats.Coin.Value -= script.Parent.Requirement.Value

			for i,v in pairs(workspace.MinimapObjects.Maps.DesertMap:GetChildren()) do
				v.Transparency = 0.75
				v.CanCollide = false
				script.Parent.SurfaceGui.TextLabel.Visible = false
			end

			if player.leaderstats.Coin.Value < script.Parent.Requirement.Value then
				player.PlayerGui.Status.NotEnoughMoney.Visible = true
				script.Parent.SurfaceGui.TextLabel.Visible = true
				wait(1)
				player.PlayerGui.Status.NotEnoughMoney.Visible = false
			end
		end
	end
end)

1 comment

0

hit.Parent isn't always the character of the player, unless you are in r6

1 answer

✓ Accepted answer

Please consider saying at what line this error fires.

I guess this is happening because you're not checking if part "hit" you're touching is actually part of character. You're just checking if hit exists. To fix it you need to check if hit is actually part of a character.

you can do it like this:

if hit.Parent:FindFirstChild("Humanoid") ~= nil then:
	-- hit is a part of character and hit.Parent is character itself.
end

ALSO!!! Please check if player exists after getting him from :GetPlayerFromCharacter() method.

if player then:

end

Also this can happen because you're making 2 variables with same name "player"

example:

local player = game:GetService("Players")
-- and
local player = game.Players:GetPlayerFromCharacter(hit.Parent)

1 comment

0

You don't necessarily need to put ":" at the end of "then".

This discussion is preserved as it was. New answers, comments, and votes are closed.