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

How to fire a remote event and detect who touched the brick whenever touch a brick?

Hey. I'm trying to fire a remote event when a player touches a brick, and then change their team. I tried to do it and it fires the remote, but the only problem is, I want to detect who touched the brick. Any examples?

1 comment

0

I did an edit to my answer, go ahead and look at it. There's not really an example as most people use GUIs instead of touch bricks as the Touched Event is very easy to break on ROBLOX.

4 answers

Ok, you do not need a remote event. But you can sense when it is touched:

workspace.Part.Touched:Connect(function(hit)
	print(hit.Parent.Name.." touched "..workspace.Part.Name)
	game.Players:GetPlayerFromCharacter(hit.Parent).Team = game.Teams.TeamName
end)

remote event must be in replicated storage

client

game.replicatedStorage.ExampleRemoteEvent:FireServer()

server

game.replicatedStorage.ExampleRemoteEvent.OnServerEvent:Connect(function()

end)

4 comments

0

This code is really wrong, client side touching is bad.

0

how is it bad, please elaborate. it's fire and forget, it doesn't yield

0

Your code is not bad but your code doesn't answer the question the poster was asking.

1

I asked how to fire a remote when touch a brick.

Hey, okay so let's start with you can't fire a normal RemoteEvent,RemoteFunction with a script because it's a client sided thing. You could make a LocalScript and fire the Remote from there when the part is touched like:

local remote = game.ReplicatedStorage.Remote
local Part = workspace.Part

Part.Touched:Connect(function()
	remote:FireServer()
end)

But i highly suggest if you have a basic knowledge of Lua to hookup a BindableEvent.

Example with the Bindable:

SERVER

local remote = game.ReplicatedStorage.Remote
remote.Event:Connect(function()
print("touched")
end)

PART SCRIPT

local remote = game.ReplicatedStorage.Remote
local Part = workspace.Part

Part.Touched:Connect(function()
	remote:Fire()
end)

PS: I suggest adding a check for humanoid so the script sure finds the user and knows it's a character touching like this:

local Part = workspace.Part
Part.Touched:Connect(function(touched)
	if touched.Parent:FindFirstChild("Humanoid")~=nil then
	print("for sure a player character")
	end
end)

(EDIT)

RemoteEvents, Functions are for the client to communicate with the server. BindableEvents. Functions are for a script to communicate with the server.

3 comments

0

Although I do like your statements but you did not give any references to your statements. This makes your answer very flawed.

0

Something you stated on another persons post was that "Client Side Touching is bad", you did not elaborate on why that is bad, plus I disagree with you. I think it just depends on what the item is for. Please make sure that when you make a comment like that, you have some type of reasoning for it that you can explain.

0

Something I recommend is when you post an answer, you give any type of references you can. A good example is the Remote Events and Functions Developer Hub Page... you could of linked that as well but you chose not to.

Remote Events

A RemoteEvent is for the Client to make a change to a server, when you touch a Brick, you need to have a script that states when the Brick is touched, it fires the event. One thing to note is that a Remote Event must be accessible to the Client, a good place to put Remote Events and so on is the Replicated Storage section.

How to fire a Remote Event

To fire a remote event, you must make sure that the script you're using to trigger the remote event is a local script. The script would go as follows. I would recommend using a BindableEvent.

Bindable Events

A Bindable Event is practically the same thing as a Remote Event but it instead works from Server Scripts to Server Scripts. No client is recorded then.

Script Inside of the Brick

-- [[This script is a Server Script.]]-- 
--[[Made By MillerrIAm]]-- 
--[[Server Variables]]-- 
local BindableEvent = game.ReplicatedStorage.Event --Change this to your BindableEvent 
--[[Main Script]]-- 
script.Parent.Touched:Connect(function()
	print("Brick Touched")
	BindableEvent:Fire("Your BindableEvent is working as acording.")
end)

Script inside of ServerStorage

--[[This is a Server Script.]]-- 
--[[Made By MillerrIAm]]-- 
--[[Server Variables]]-- 
local BindableEvent = game.ReplicatedStorage.Event --Change this to your BindableEvent
--[[Main Script]]-- 
BindableEvent.Event:Connect(function(value)
	print(value)
	wait(5)
end)

Last note

I recommend doing a Humanoid check to see if the Humanoid is actually touching. Here's how i'd do it for the script inside the brick....

--[[Made By MillerrIAm]]-- 
--[[Server Variables]]-- 
local BindableEvent = game.ReplicatedStorage.Event
--[[Main Script]]-- 
script.Parent.Touched:Connect(function(part)
	local plr = part.Parent:FindFirstChild("Humanoid")
	if plr then
		print("Brick Touched")
		BindableEvent:Fire("Your BindableEvent is working as acording.")
	end
end)

Answer to the Question

The best way to do this is have the local script is to have your local script record the player who's touching and or firing the script. I'm not at my computer at the moment so I can't really show you an example but I'd recommend using a GUI system instead as it's more efficient than using a Touch Brick.

3 comments

0

I want to detect who touched the brick, to change their team, any help?

0

Oooohhh okay. Give me a moment.

0

Eh great job on stealing my answer :D

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