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

Why does the output print "Torso is not valid member of model"?

Asked by
phim37 4
5 years ago

While I was testing my code, I encountered a problem when testing it on a local server. The code below is placed in a local script inside the PlayerGUI

local plr = game.Players.LocalPlayer
local char = plr.Character

char.Torso.Touched:connect(function(hit)
    print("Rome")
end

When the function is activated on a local server, It will not print Rome but instead return "Torso is not a valid member of Model". I find this problem extremely odd. All help is appreciated.

2 answers

Log in to vote
0
Answered by
CPF2 406 Moderation Voter
5 years ago
Edited 5 years ago

It is most likely because you are assuming the character is already loaded, try this solution.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

char:FindFirstChild('Torso').Touched:Connect(function(hit)
    print("Rome")
end
0
It's :Wait not :wait. :Connect not :connect. Don't give deprecated code. User#19524 175 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

This is likely because you expected the character to be loaded in immediately. This is not always the case. You should wait for the character using CharacterAdded.

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

char.Torso.Touched:Connect(function(hit) -- :connect is deprecated, switch to :Connect
    print("Rome")
end

Answer this question