Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I have a Error "attempt to index nil with 'leaderstats' " can you help me ?

Asked by 3 years ago

So i want to make a sell part the script looks like this :

script.Parent.Touched:Connect(function(hit)
    if hit.parent:FindFirstChild("Humanoid") then 
        local plr = game.Players.LocalPlayer
        local Cash = plr.leaderstats.Cash
        local Stren = plr.leaderstats.Strength

        Cash.Value = Cash.Value + Stren.Value
        Stren.Value = 0
    end
end)

Why a have this error ?

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Ok, So Here The Problem

local plr = game.Players.LocalPlayer(Line 3).

To search LocalPlayer, you cant use Script. You must use LocalScript! BUT, if you still want to use your Script you can use this function:

GetPlayerFromCharacter

This function returns the Player associated with the given Player.Character, or nil if one cannot be found.

script.Parent.Touched:Connect(function(hit)
    if hit.parent:FindFirstChild("Humanoid") then 
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Just Change This
        local Cash = plr.leaderstats.Cash
        local Stren = plr.leaderstats.Strength

        Cash.Value = Cash.Value + Stren.Value
        Stren.Value = 0
    end
end)

Hope It Helps!

0
Thanks you are very HelpFul :D SamSoGames 7 — 3y
0
@SamSoGames Np Mate! The_Saver31 260 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Is it a local script? You can only access the LocalPlayer from a local script

https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer

Answer this question