Log in to vote

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

Asked by 14 days ago

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)
hit.Parent isn't always the character of the player, unless you are in r6 greatneil80 2641 — 14d

1 answer

Log in to vote
Answered by 14 days ago
Edited 14 days ago

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.

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

if player then:


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


local player = game:GetService("Players")
-- and
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
You don't necessarily need to put ":" at the end of "then". T3_MasterGamer 1017 — 13d

Answer this question