Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Any method for getting the character who touched the brick using ".Touched"?

Asked by 10 days ago
Edited 10 days ago

Hello,I am trying to write a code that fires a client when you touch a part. Here is my code:

script.Parent.Touched:Connect(function()
    game:GetService("ReplicatedStorage").Events.Pt:FireClient()
end)

The only problem is that FireClient() needs a parameter,of who it will fire to,and I want it to fire to the player that touched the part.Anyway to do this? This code here dosent work:

script.Parent.Touched:Connect(function(hit)
    game:GetService("ReplicatedStorage").Events.Pt:FireClient(hit)
end)

Help please

2 answers

Log in to vote
0
Answered by 10 days ago

i think this will work

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    game:GetService("ReplicatedStorage").Events.Pt:FireClient(hit.Parent)
end)

it will get the player name from the character that touched the part and then it will fire that character in the FireClient

im not 100% sure this will work but i think it will

0
also with that u can do whatever u want with the local player Gameplayer365247v2 166 — 10d
0
Will this work? RBLXNogin 4 — 10d
0
Will this work? RBLXNogin 4 — 10d
0
the hell is this srsly incapaxx 2925 — 9d
View all comments (3 more)
0
i said i think it will work, im not sure cause i didnt test it Gameplayer365247v2 166 — 9d
0
Gameplayer365247v2, your way is too complicated Owengren 30 — 9d
0
no Gameplayer365247v2 166 — 9d
Ad
Log in to vote
0
Answered by 9 days ago

I hope this will help you:

local p = script.Parent
local charName = nil
p.Touched:Connect(function(part)
    if part.Parent.Humanoid ~= nil then
        charName = part.Parent.Name
    end
end)

Use the charName variable to get the name of the character. To get the character as an object, do this:

local char = nil
if charName ~= nil then
    char = workspace[charName]
end

Answer this question