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

How do I change leaderstats value?

it errors and says I’m trying to index nil with leaderstats. How would I go about fixing this?

local p1 = script.Parent.P1
local player = game.Players.LocalPlayer

local function onEat(otherPart)
	local hum = otherPart.Parent:FindFirstChild("HumanoidRootPart")

	if hum then
			
		script.Parent:ClearAllChildren()
		
		player.leaderstats.Mass.Value = player.leaderstats.Mass.Value + 1
		
		hum.Size = hum.Size + Vector3.new(0.5, 0.5, 0.5)
		
		local Children = otherPart.Parent:GetChildren()
		
		for i, v in pairs(Children) do -- i, v in pairs loop to sort through table of children and find objects named "Part"
			if v.Name == "Part" then
				v.Size = v.Size + Vector3.new(0.5, 0.5, 0.5)
			end
		end
	end
end

script.Parent.P1.Touched:Connect(onEat)

1 answer

✓ Accepted answer

The server can't access LocalPlayer. You can use the player's character to find their player

local p1 = script.Parent.P1

local function onEat(otherPart)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if not player then return end
    local hum = otherPart.Parent:FindFirstChild("HumanoidRootPart")

    if hum then

        script.Parent:ClearAllChildren()

        player.leaderstats.Mass.Value = player.leaderstats.Mass.Value + 1

        hum.Size = hum.Size + Vector3.new(0.5, 0.5, 0.5)

        local Children = otherPart.Parent:GetChildren()

        for i, v in pairs(Children) do -- i, v in pairs loop to sort through table of children and find objects named "Part"
            if v.Name == "Part" then
                v.Size = v.Size + Vector3.new(0.5, 0.5, 0.5)
            end
        end
    end
end

script.Parent.P1.Touched:Connect(onEat)

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