So I'm making a computer console that you can walk up to and press F to use it. This will then open up a UI.
Here's my script:
local UIContainer = script:GetCustomProperty("UIContainer"):WaitForObject()
local ConsoleTrigger = script:GetCustomProperty("Trigger"):WaitForObject()
-- open/close UI with the F key if player is overlapping trigger
function OnBindingPressed(player, action)
if (ConsoleTrigger:IsOverlapping(player)) then
if action == "ability_extra_33" then
if UIContainer.visibility == Visibility.FORCE_OFF then
UIContainer.visibility = Visibility.FORCE_ON
elseif UIContainer.visibility == Visibility.FORCE_ON then
UIContainer.visibility = Visibility.FORCE_OFF
end
end
else
UIContainer.visibility = Visibility.FORCE_OFF
end
end
function OnPlayerJoined(player)
player.bindingPressedEvent:Connect(OnBindingPressed)
end
Game.playerJoinedEvent:Connect(OnPlayerJoined)
It works fine except for 1 thing.. If I enter the trigger area and press the F key to open the UI and then run away, the screen still stays even though I have left the trigger area.
Does anyone have any suggestions on how to make the UI automatically go away when leaving the trigger area?
I also tried doing this with OnBeginOverlap and OnEndOverlap instead of IsOverlapping, but that just caused other issues.
In your script you're just checking for if they are overlapping at the time they press; you haven't yet set anything up that performs an action when they leave.
You can do this by connecting the event endOverlapEvent, and connecting that to a function that closes the popup.
The good news is, with the trigger system in Core you don't need to code your own bindingPressedEvent logic. You can use the interactedEvent instead.
Note that I'm assuming this is all in a client script. If you're in a default or server script, you'll need some additional logic to make sure OTHER players don't activate it.
local UIContainer = script:GetCustomProperty("UIContainer"):WaitForObject()
local ConsoleTrigger = script:GetCustomProperty("Trigger"):WaitForObject()
-- Function fires when player interacts with the trigger.
function OnInteracted(trigger, player)
if UIContainer.visibility == Visibility.FORCE_OFF then
OpenConsole()
elseif UIContainer.visibility == Visibility.FORCE_ON then
CloseConsole()
end
end
function OnEndOverlap(trigger, object)
-- This checks to make sure the object interacting with the trigger is a player. Since we're running in a client script, you don't need to confirm it is the local player.
if object:IsA('Player')
CloseConsole()
end
end
-- Sets visibility to on
function OpenConsole()
UIContainer.visibility = Visibility.FORCE_ON
end
-- Sets visibility to off
function CloseConsole()
UIContainer.visibility = Visibility.FORCE_OFF
end
-- Connects interacted event to OnInteracted function
ConsoleTrigger.interactedEvent:Connect(OnInteracted)
-- Sets it so that OnEndOverlap always fires when you end overlapping
ConsoleTrigger.endOverlapEvent:Connect(OnEndOverlap)
Thanks a bunch! But I'm a little confused on how the client/server stuff works.
This is a client script. My goal with this is for the UI to give the player a way to choose a weapon to equip. So I figured a client script would be best for that?
I am testing it in Multiplayer Preview Mode with 4 players. I walk one bot up to the terminal and it opens the UI, then I get another one to open the UI there. This works fine mostly. Both bots can open and close the UI on their own screen, but if 1 bot has the UI open and the other bot leaves the trigger area, the UI for both bots closes.
I assumed that since it's a client script the actions of the other bots wouldn't affect each other?
I put a print statement in the functions to help me see what's happening. In OnEndOverlap, each of the 4 bots prints the announcement. I saw that you mentioned checking for local player inside of OnEndOverlap, but is there a way to do this without having all players notified when any player exits the trigger area?