Ad
Log in to vote
1

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

1 answer

Log in to vote
0
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.
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)
0
You don't necessarily need to put ":" at the end of "then". T3_MasterGamer 1017 — 13d
Ad

Answer this question