Local Event Listener (Not using Server)?

I’m trying to make a game where if a person has been in a room (touched the baseplate of the part basically) that it pops up a message on a screen saying that, that room has been checked.

Is there a way to do this locally and not using the Server for any event listening?

I got something like this:

    local checkedRooms = {};

    local roomChecked = Instance.new("BindableEvent");
    
    local function onRoomChecked(roomName);
        table.insert(checkedRooms, roomName);
    end

    roomChecked.Event:Connect(onRoomChecked);

How could I implement something like this to make it work on the local level?

Touched will fire locally in a local script.

Is there a way to make an event listener for all the rooms in one single script instead of adding a localscript into each part?

Yes, you’d just have to correctly reference each part from the same single script.

Could you show me an example? I’ve been stuck on this part of the map for the past hour and a half now.

local part1 = workspace:WaitForChild("Part1")
local part2 = workspace:WaitForChild("Part2")

part1.Touched:Connect(function()
	--do stuff
end)

part2.Touched:Connect(function()
	--do stuff
end)

Primitive but this is essentially the logic.

1 Like

Would this fire if, say, another player in my game touches the part?

Yes but it’ll fire for them locally too.

One more question for you, if you don’t mind. >_>

I have about 30 rooms. How would you go about making an event listener for all the rooms instead of initializing every single one of them?

local room1 = blah blah blah1
local room2 = blah blah blah2
local room3 = blah blah blah3
local room4 = blah blah blah4

room1.Touched:Connect(function()
end)
room2.Touched:Connect(function()
end)
-- and so on
local rooms = workspace:WaitForChild("Rooms") --lets say this is a folder which contains the room floor parts
local roomTriggers = rooms:GetChildren() --create an array of the folders children

for _, trigger in ipairs(roomTriggers) do --iterate over the items of the array
	trigger.Touched:Connect(function() --connect the same callback function to the touched event for each room
		--do stuff
	end)
end
3 Likes