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
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
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()