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)
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)
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)
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)
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
Good Luck!