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
TorsoPart does not exist, it probably has different name, you might be looking for "UpperTorso", or "LowerTorso", OR "Torso".
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.