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

How to fix "Attempt index nil with 'leaderstatstwo' "?

Asked by 2 years ago
Edited 2 years ago

I'm having a problem with one of my scripts. I have it coded to where every time you pick up this chicken it should give you a point every second. I have this set up and working for kills, however when I do it with the point system it gives me an error that says, "attempt to index nil with 'leaderstatstwo' ". I don't know what the problem is. This is the script inside the chicken, where the error is occuring.

while true do
            if script.Parent.Count.Value == true then
                local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent)
                wait(1)
                plr.leaderstatstwo.ChickenLB.Value = plr.leaderstatstwo.ChickenLB.Value + 1
            end
            wait()
        end

This is the script that spawns the folder inside the player.

game.Players.PlayerAdded:Connect(function(player)
    local foldertwo = Instance.new("Folder",player)
    foldertwo.Name = "leaderstatstwo"
    local chickenlb = Instance.new("IntValue",foldertwo)
    chickenlb.Name = "ChickenLB"
end)

Third script found inside startergui>ScreenGUI>TextLabel, this is the local script.

while wait() do
    local player = game.Players.LocalPlayer
    script.Parent.Text = "Chicken Points: "..player:WaitForChild("leaderstatstwo"):FindFirstChild("ChickenLB").Value
end

It even shows the folder and the IntValue whenever you play and look under the player's folder.

0
EDIT: It spawns a folder inside the player. The script however is inside the workspace. harryturtleface 0 — 2y
0
is the first code inside a normal or local script? datboyPiter 52 — 2y
0
Normal script harryturtleface 0 — 2y

3 answers

Log in to vote
0
Answered by 2 years ago

The local script can't fire inside the workspace: https://developer.roblox.com/en-us/api-reference/class/LocalScript

0
It's not. The first script is a normal script in worlspace. The second is a local script inside the starter gui. However, I forgot to mention there is a third script inside the chicken. harryturtleface 0 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Local scripts cannot fire when in workspace. There is an article on the developer hub stating this.

Log in to vote
0
Answered by 2 years ago

GetPlayerFromCharacter() will return nil if the Player is not found. To stop this you must add a check to see if it has not returned nil.

if not plr then return end
0
I put the code you showed right under plr.leaderstatstwo.ChickenLB.Value = plr.leaderstatstwo.ChickenLB.Value. It then gives me the same error 5 times then stops and does nothing harryturtleface 0 — 2y
0
you need to put it under where you first define player notoriousNoah10 5 — 2y

Answer this question