Firing Back to Server Not Working

Hello DevForums, I’m trying to make it so that when you touch 3 different blocks, they activate a portal. The one problem is that I came across an issue. Everything in the script works besides the ending part.

Script:

local rp = game.ReplicatedStorage

local t1 = false
local t2 = false
local t3 = false

rp.Events.Touch1.OnClientEvent:Connect(function()
’ ’ ’ print(“recieved touch1”)
’ ’ ’ t1 = true
end)

rp.Events.Touch2.OnClientEvent:Connect(function()
’ ’ ’ print(“recieved touch2”)
’ ’ ’ t2 = true
end)

rp.Events.Touch3.OnClientEvent:Connect(function()
’ ’ ’ print(“recieved touch3”)
’ ’ ’ t3 = true
end)

if t1 == true and t2 == true and t3 == true then
’ ’ ’ print(“all 3 blocks touched”)
’ ’ ’ rp.Events.PortalOpen:FireServer()
end

There is no error in the output but it doesn’t print the “all 3 blocks touched” nor does it fire the server event.

The problem is that your code is running sequentially rather than from events.

This block of code runs IMMEDIATELY after the script is loaded into the game, and you set the values of t1, t2, and t3 to false at the beginning of your code. In order to check to see if all variables are true, you can create a function that checks if ALL variables are true and run them along each OnClientEvent event.

It could look something like this (the function):

function check()
   if t1 and t2 and t3 then
      print('all blocks touched')
      --Fire your events here
   end
end

and you can just fire that function on each event :slight_smile:

To go in more depth with this, here’s a lengthy Sequential driven programming vs. Event driven programming: Unit 5 Lesson 1: Lists Explore

In addition to what ijmod brought up, this code should be run from a script (not a local script). I noticed you used FireServer(), which should only be used in a local script. If you want to let other player’s clients know about something from the server, use FireAllClients() or FireClient(). You can also signal other scripts on the server with a BindableEvent and the Fire() function.

If you aren’t getting any messages and your code is being run from a script, there is probably an error in your blocks’ code.

Try this. It should yield the script until all three events are fired in any order:

local rp = game.ReplicatedStorage

local t1 = false
local t2 = false
local t3 = false

local continue = Instance.new("BindableEvent")

rp.Events.Touch1.OnClientEvent:Connect(function()
	print("recieved touch1")
	if (t2 and t3 and continue) then
		continue:Fire()
	end
	t1 = true
end)

rp.Events.Touch2.OnClientEvent:Connect(function()
	print("recieved touch2")
	if (t1 and t3 and continue) then
		continue:Fire()
	end
	t2 = true
end)

rp.Events.Touch3.OnClientEvent:Connect(function()
	print("recieved touch3")
	if (t1 and t2 and continue) then
		continue:Fire()
	end
	t3 = true
end)

continue.Event:Wait()
continue:Destroy()
continue = nil

print("all 3 blocks touched")
rp.Events.PortalOpen:FireServer()
1 Like

The bindable event will get destroyed after the calling it the first time.

It is no longer needed once the code is done yielding.

Have you tried checking to see if the gun is loaded or maybe your out of bullets?