Is there any 'when' function?

Hey, I just had a simple question. When trying to figure out when something happens I always do

while true do
	wait()
	if ... then
		print("Hello World!")
	end
end

Or repeat wait() until ...
but is there no other way? these all seem bad for performance, i know there is not literally a when function, but is there something else built into Roblox?

I am a bit confused. Is that what you’re looking for?

2 Likes

there’s :WaitForChild() if that’s what you mean.

Also use task.wait() instead of wait()

1 Like

When i usually wait for something to exist, or happens. I use

while true do
	wait()
	if (instance that has to exist) then
		print("Hello World!")
	end
end

Is there any way to do this? I know for parts or something u have :WaitForChild(), but if I want to make it so that it waits for player to be in a certain position I use the loop above.

1 Like

For parts u have :WaitForChild() yes, but for example to check whenever a player is at a certain position? Like when the HumanoidRootPart.Position = Vector3.new(0,5,0)

I think you’re looking for signal behaviour, but bare in mind at some point you will need something like your previous code snippet.

You can use BindableEvents (or I would recommend just making your own signal class), and have a script fire events when the conditions are met. Other scripts can listen for these events and perform actions based off of them. It’s more of an optimisation - having one iteration that checks a bunch of conditions is better than having a bunch of iterations.

Take this example:

--say this script was already in use for something else

local runService = game:GetService("RunService")
local Signal = require("../path-to-module")

local PlayerHitPositionSignal = Signal.new()

runService:BindToRenderStep("something", 2000, function()
    --other logic that would be here either way

    --signal logic
    if (HumanoidRootPart.Position.Y == 50) then --say you want the player to hit Y position 50
        PlayerHitPositionSignal:Fire(HumanoidRootPart.Position)
    end
end)
--then, in your other script, you can have code to run on this
local Event = --reference to the same event

Event:Connect(function(hrpPos: Vector3)
    print("Player hit Y position 50. Current full position: "..tostring(hrpPos))
end)

Root part position might have been a bad example, but this can be used for multiple things in the sense that it’s not just for instances - this can, in a way, act as your when function.

1 Like

I guess something like this?

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()
local pos = Vector3.new(0,3,0)

local func

func = Character.PrimaryPart:GetPropertyChangedSignal("Position"):Connect(function()
	if Character.PrimaryPart.Position == pos then 
		print("player is in position.")
		func:Disconnect()
	end
end)

This is a very fragile and likely to malfunction solution, as the player can be as little as 1cm away from that position, and your function will not trigger. You should use an invisible part or a zone to detect when the player is in it.

Ah i guess this works for positions, i wonder if there are any other examples where its not this easy, Thanks!

1 Like

Yeah it was just an example to try to explain what i was trying

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.