Answered by
4 years ago Edited 4 years ago
Because your script is not correct, attempt to index nil
means you did
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:
01 | game.ReplicatedStorage.Event 1. OnClientEvent:Connect( function () |
02 | local player = game.Players.LocalPlayer |
03 | local char = player.Character |
12 | char.TorsoPart.ProximityPrompt.Enabled = false |
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.
01 | game.ReplicatedStorage.Event 1. OnClientEvent:Connect( function () |
02 | local player = game.Players.LocalPlayer |
06 | local char = player.Character or player.CharacterAdded:Wait() |
14 | char.TorsoPart.ProximityPrompt.Enabled = false |
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
1 | local Something = nil or false |
3 | local String = "String" or "String2" |
4 | local Bool = false or true |
6 | print (Something, Number, String, Bool) |
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.