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

Why is this error coming up if my script is correct?

Asked by 3 years ago

Error: attempt to index nil with 'TorsoPart Code: Local Script ?

game.ReplicatedStorage.Event1.OnClientEvent:Connect(function(plr)
    local player = game.Players.LocalPlayer.Name --Find Player name
    local char = game.Workspace:FindFirstChild(player) --Get the player in workspace

    print("Flipped")
    wait(6)
    char.TorsoPart.ProximityPrompt.Enabled = false
    end)

Before this fires i have code that clones a part in the workspace which has a proximity prompt inside it, it clones it into the players model and positions inside of the players uppertorso.

so all i am looking for is why is my code not working even though when i manually check in the explorer tab the TorsoPart is inside of the model

2 answers

Log in to vote
0
Answered by 3 years ago

TorsoPart does not exist, it probably has different name, you might be looking for "UpperTorso", or "LowerTorso", OR "Torso".

0
i understand the error but i created the part and put it inside of the players model so its there. and if you check through the explorer tab you can see its there TeaWithMee 76 — 3y
0
There is no 'TorsoPart' in Characters. You are probably looking for 'UpperTorso' or 'LowerTorso' (in R15 Characters) or for 'Torso' (in R6 Characters). Jermartynojm 0 — 3y
Ad
Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Because your script is not correct, attempt to index nil means you did

nil.TorsoPart....

Which is not acceptable in Luau. Your char variable is nil which means the gamers character is not yet spawned. You have few problems in your code, first is at at the first line and that's OnClientEvent does not return plr argument because you know who the LocalPlayer is, second is that the way you get Character is by accessing Player.Character, doing workspace:FindFirstChild(player.Name) is bad. It will work fine in most cases, but it's not preferred.

To fix that you need to check if the character exists or wait until your character gets spawned, i don't know your situation so you need to choose one. So first one is to check if character is not nil:

game.ReplicatedStorage.Event1.OnClientEvent:Connect(function()
    local player = game.Players.LocalPlayer -- get the Player, not his name
    local char = player.Character -- refers to player's character or nil if character is not in the world

    print("Flipped")
    wait(6)

    --/Simple if statement will prevent your script from erroring because
    --\it will only pass if given condition is not false and not nil. In this case
    --\it will pass if [char] is not nil, exactly what you need.
    if (char) then
        char.TorsoPart.ProximityPrompt.Enabled = false
    end
end)

This will prevent the script from erroring but this might not be what you wanted, let's say you want to wait for the character to add, this is why you use CharacterAdded event, it fires when character spawns and returns the character.

game.ReplicatedStorage.Event1.OnClientEvent:Connect(function()
    local player = game.Players.LocalPlayer

    --/Will refer to current character OR will wait until new character
    --\is spawned and refer to it
    local char = player.Character or player.CharacterAdded:Wait()

    print("Flipped")
    wait(6)

    --/If statement is still needed because after waiting 6 seconds
    --\state of the character could have changed
    if (char) then
        char.TorsoPart.ProximityPrompt.Enabled = false
    end
end)

In case your character does not exist yet, it will yield until CharacterAdded fires because i used :Wait(), it's a function of RBXScriptSignal which yields until it fires and returns what the signal returns, in this case it returns character.

or statement is very simple

local Something = nil or false
local Number = nil or 5 
local String = "String" or "String2"
local Bool = false or true

print(Something, Number, String, Bool)
-- false, 5, "String", true

In case of variables it checks if first given statement is not false and not nil, if it's not it will assign variable to it, but if it is false or nil then it will assign variable to value given after the or, no matter what it is, nil or false, it will assign it.

0
the 2nd one down works like my original one but when i reset or get killed the error comes up. even though i have it so the part inserts into the player every time they respawn or come into the game TeaWithMee 76 — 3y
0
Oh right because when you reset your character does not become nil, it's parented to nil instead, so you need to check if char.Parent is not nil on line 13 in my second example; change it to: if (char.Parent) then ... imKirda 4491 — 3y
0
that works, there is no error but when i respawn nothing happens even though it worked the first time when i first joined TeaWithMee 76 — 3y
0
theen you need to make sure you are actually firing [Event1], it's possible that something is wrong with the script firing it. imKirda 4491 — 3y

Answer this question