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

(FilteringEnabled) event Touched doesn't work?

Here are the basic scripts I made. Output should print the player's who touched the part name but it doesn't say anything... (LocalScript is in a Part. Script and RemoteEvent are in LocalScript.)

LocalScript:

script.Parent.Touched:connect(function()
	script.Parent.LocalScript.RemoteEvent:FireServer()
end)

Script:

script.Parent.RemoteEvent.OnServerEvent:connect(function(Player)
	print(Player.Name)
end)

2 comments

1

You do not need a remote event for the touched event, this can be done by a server script wiht FE on.

0

Thank you, I'll try.

1 answer

✓ Accepted answer

Local Scripts don't work in workspace

Local Scripts don't work in workspace, unless they're in a character.

For your script, as Kingdom5 mentioned, you don't need to use a Remote Event. Remote events are used for Server to Client, or Client to Server communication. Because we can do everything in a Regular Script, we don't need remote functions.

-- Regular Script in Part, in workspace

script.Parent.Touched:connect(function(part)
	print(part.Name)
end)
Prints the name of the part that touched the object

Getting Characters from a Touched Event,

Sometimes you might want to get a character that touches the part. How do we know it's a Character? Well, all characters have a thing called a Humanoid inside them. All we have to do, is check if the parent of the part has a Humanoid.

-- Regular Script in Part, in workspace

script.Parent.Touched:connect(function(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")-- looks for humanoid
	if humanoid then-- checks for humanoid
		local character = part.Parent -- Gets the now certain character
		print(character.Name)
	end
end)
Prints the name of the character, if there is one, that touched the object

Getting a Player from a Touched Event

The other one works for NPCs too. It works for anything with a Humanoid, and sometimes we want the Player, not the Character. We can use the :GetPlayerFromCharacter function of the Players to do this very easily.

-- Regular Script in Part, in workspace

script.Parent.Touched:connect(function(part)
	local plr = game.Players:GetPlayerFromCharacter(part.Parent)-- Gets player
	if plr then-- Checks if player exists 
		print(plr.Name)-- Prints player name
	end
end)
Prints player name

If we wanted to get the character and only if it's a player, we can use .Character on any player. Example,

local plr = "DefinePlayer")
local char = plr.Character
Gets character from player

Good Luck!

if I helped, please don't forget to accept my answer

3 comments

0

lol, he never accepted your answer. rip

0

I did. Thank you very much!

0

No problem :3

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