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

(FilteringEnabled) event Touched doesn't work?

Asked by
4inis 15
8 years ago
Edited 8 years ago

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)
1
You do not need a remote event for the touched event, this can be done by a server script wiht FE on. User#5423 17 — 8y
0
Thank you, I'll try. 4inis 15 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

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
0
lol, he never accepted your answer. rip konichiwah1337 233 — 8y
0
I did. Thank you very much! 4inis 15 — 8y
0
No problem :3 User#11440 120 — 8y
Ad

Answer this question