How to fire a remote event when a part is touched

i would like to know how to fire a remote event when a part is touched

4 Likes

you can do

part.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        -- this will run on the server when a player touches the part
    end
end)
2 Likes

i would like it to fire a remote event

So just fire a remote event where I said “this will run on the server when a player touches the part”
I have a tutorial on how to use remote events here

Here

local PartName = game.workspace.PartName -- name of part
PartName.Touched:Connect(function)() -- Touched is the event and :connect is connecting a function the the function starts.
Print("Part is touched") -- prints part is touched
end)

To make it run a RemoteEvent, simply inside of what @AccessQ mentioned, write the location of your RemoteEvent and fire it

ok thanks :smile: :grinning: :smiley:

Make a remote in the replicated storage

script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		Events.RemoteEvent:FireServer(Function)
	end
end)

where should i put this script

It should be parented inside your part, this script is a local script.

remote events only work in local scritps

This is very easy to do, put a script into your part with this script into it if you want to fire it to the client:

script.Parent.Touched:Connect(function(hit) -- The part has been touched
    if hit.Parent:FindFirstChild("Humanoid") then -- The touched's parent has a humanoid in it

         local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player by the character (the hit.Parent is our player in this case)

         if player then -- If the player is here
            game.ReplicatedStorage["Event Name Here"]:FireClient(player) -- Put in the event name your remote event's name, this fires the event.
         end

    end
end)

If you want to fire it to the server, create a localscript into playergui, then type this code into it:

local part = workspace.Part -- Your part's location here

part.Touched:Connect(function()
     if hit.Parent:FindFirstChild("Humanoid") then

         local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player by the character (the hit.Parent is our player in this case)
        
         if player and player == game.Players.LocalPlayer then -- Check if the player is in the game and if the player touched it is our player
             game.ReplicatedStorage["Event Name Here"]:FireServer() -- Put in the event name your remote event's name, this fires the event.
         end

     end
end)

I hope this helped you, I didn’t have time to test it yet, so tell me if you have any questions or if you found any bugs.

13 Likes

i fixed what i said was giving a problem u should have been

part.Touched:Connect(function(hit)local part = workspace.Part -- Your part's location here

part.Touched:Connect(function(hit )
     if hit.Parent:FindFirstChild("Humanoid") then

         local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Getting the player by the character (the hit.Parent is our player in this case)
        
         if player and player == game.Players.LocalPlayer then -- Check if the player is in the game and if the player touched it is our player
             game.ReplicatedStorage["Event Name Here"]:FireServer() -- Put in the event name your remote event's name, this fires the event.
         end

     end
end)

in the local script

2 Likes

I said it was in a local script, but glad it helped!

Typos:
workspace keyword, not game.‘workspace’, workspace is not lowercase
‘Print’ is not a function, use lower case ‘print’

I know I was doing it quick I am sorry my bad.