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

"attempt to index nil with 'Value'" error. How do I fix this?

Asked by 2 years ago

The script's intended purpose is to detect when its parent object is touched, then add 1 to an IntValue contained in StarterCharacterScripts. I get the aforementioned error on line 13. Shouldn't I be able to access the "Value" property like I can access "Color" and "Transparency" on things such as parts? And yes, I know there is a bit of spaghetti going on here.

stimPack = script.Parent

local function onPartTouch(otherPart)

    local partParent = otherPart.Parent 
    local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")

    if humanoid then

        local character = humanoid.Parent
        local player = game.Players:GetPlayerFromCharacter(character)
        local stims = player:FindFirstChild("Stims")
        local stimvalue = stims.Value

        print(player, "has picked up a Syringe!")

        stimValue += 1

    end

end

stimPack.Touched:Connect(onPartTouch)
0
i dont think its the value that has the problem but the stims var proROBLOXkiller5 112 — 2y
0
this is how i see it: your stims value is in the character but your stims var searches the Player and therefore the game couldnt find stims because stims is never there in the player because its in the character proROBLOXkiller5 112 — 2y
0
if the game couldnt find something that exists it is automatically set to nil and the game gets "nil.Value" proROBLOXkiller5 112 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

line 12

 local stims = player:FindFirstChild("Stims")

why are you searching inside the player when Stimsvalue is in the character

it would make more sense to do

local stims = character:FindFirstChild("Stims")
Ad

Answer this question