Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

0

It's :Wait not :wait. :Connect not :connect. Don't give deprecated code.

###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

This discussion is preserved as it was. New answers, comments, and votes are closed.