Is there a more efficient way?

Hi! I am trying to make a game where there are 100 randomly generated rooms and darkness is chasing you at times. I tried to tween it from one end of the rooms to the other and it’s really inefficient. If you get a signal that the darkness is coming, you don’t want to be hiding for 3 minutes waiting for it to come. Is there just a generally better, less laggy, more realistic solution to this? Here is my code:

local coming = false
local cooldown = math.random(10,10)
local tp = script.Parent
local tpf = workspace.DarknessEnd
local tweenservice = game:GetService("TweenService")
local ti = TweenInfo.new(300, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local tween = tweenservice:Create(tp,ti,{CFrame = tpf.CFrame})
while true do
	wait(cooldown)
	coming = true
	cooldown = 1000
	tween:Play()
	print("moved")
end

As you can see: (the black bit that looks like nothing is the darkness aka the gap between the rooms)


It’s got quite a long way to go…
Any help to make this feature better in general?

1 Like

Are you able to explain what the “darkness” is? Is it just the light turning off or an entity?

1 Like

If you’re talking about efficiency, I would definitely suggest using coroutines, mainly because of the while true loop in main thread.
I don’t exactly know what you are trying to achieve, however I’d never suggest anyone tweening a part on server side. It’s always better to send a remote event to all clients that the part should be tweened, since server movement makes it look really laggy (For details read about network ownership).
If the darkness should be hurting people in it, simply make a calculation where the darkness should be now based on time, and if some players are standing in such area, do an action.

1 Like

Sorry for the late response. ‘Darkness’ is basically like a black wall coming for you.

Instead of using:

while true do
   wait(10)
end

Use this instead:

while task.wait(10) do

end
2 Likes

There are a few ways you could improve the performance of the script and make the movement of the darkness more realistic:

  1. Instead of using a TweenService to animate the movement of the darkness, you could use a BodyMover object and set its Velocity property to move the darkness from one end of the rooms to the other. This would allow you to control the speed of the darkness more precisely, and the movement would be more realistic.
  2. You could also consider using a pathfinding algorithm to navigate the darkness through the rooms. This would allow the darkness to move more naturally, avoiding obstacles and finding the most efficient path.
  3. To make the movement of the darkness more dynamic, you could add some variation to the speed and direction of the darkness. For example, you could randomize the speed of the darkness, or make it change direction at certain points.
  4. Instead of waiting for a fixed amount of time before moving the darkness, you could consider using a BodyMover object and set its MaxForce property to a low value to simulate the darkness gradually accelerating over time. This would make the movement of the darkness feel more natural.

I hope these suggestions help! Let me know if you have any questions or if you need further assistance.

local coming = false
local cooldown = math.random(10,10)
local tp = script.Parent
local tpf = workspace.DarknessEnd
local bodyMover = Instance.new("BodyMover")

while true do
	wait(cooldown)
	coming = true
	cooldown = 1000
	
	-- Set the target position and velocity of the darkness
	bodyMover.MaxForce = Vector3.new(10000, 10000, 10000)
	bodyMover.P = 10000
	bodyMover.D = 50
	bodyMover.Parent = tp
	bodyMover.Target = tpf
	
	print("moved")
end

This script will move the darkness from its current position to the position of tpf at a constant speed. You can adjust the MaxForce and P properties of the BodyMover object to control the acceleration and speed of the darkness.

I hope this helps! Let me know if you have any questions or if you need further assistance.

Ah yes, nothing says “realistic” like having darkness chase you around in a game. And I’m sure the 3 minute tween time is totally reasonable and not at all frustrating for the player. And who wouldn’t love to wait around for 1000 seconds between each chase sequence? Sounds like a blast! Keep up the fantastic work.

This is not a proper method and is subject to break if Roblox changes their wait function. As of now, wait returns a value, but this may not always be the case.

coroutine.wrap(function()
  while true do
    -- TODO: Code
    task.wait(10)
  end
end)()