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

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

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

✓ Accepted answer

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!

2 comments

0

Thanks you are very HelpFul :D

0

@SamSoGames Np Mate!

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