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

Touch brick change color question about script itself?

on line 1, after the function is obj always necessary to have ? and on line 2 find first child the only way to detect a players touch ?

game.Workspace.Part.Touched:Connect(function(obj)    -- is obj always necessary -- 

    if obj.Parent:FindFirstChild("Humanoid") then  -- Is FindFirstChild the only why find a player?--

        game.Workspace.Change.BrickColor = BrickColor.new("Really Black")

    end

end)

I am just wanting to learn more about this script, the content in the lines mentioned are new to me as I am learning; and would appreciate some incite .

1 comment

0

Locking to prevent bad answers

1 answer

✓ Accepted answer

1. Do you need the obj parameter?

No, you do not: this is optional. But if you need the part that touched your other part, then you would add it.

2. Do you need :FindFirstChild call?

No. Parts that do not belong to a player's character can touch parts aswell. Like the baseplate, for example.

In this example, it is looking for a humanoid. This method is kinda old and isn't as reliable, since anything can have a humanoid and still pass the conditional statement.

A more reliable way exists: Players:GetPlayerFromCharacter.

An instance is expected as an argument, and if it is a player's character, the player instance is returned. If the object passed was not a player's character, nil is returned.

local Players, Workspace = game:GetService("Players"), game:GetService("Workspace");

Workspace.Part.Touched:Connect(function(part) -- I like part as it is clearer
	local client = Players:GetPlayerFromCharacter(part.Parent);

	if (client) then
		Workspace.Change.BrickColor = BrickColor.new("Really black");
	end
end);

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