I'm working on a little game where every 60 seconds there's a chance a monster will wake up and teleport to players, instakilling them. The player can avoid this by getting into a locker before he kills them. The problem is, whenever every player is hiding, the loop I use to get all the players executes too many times and lags. Is there a way to fix this?

Below is the code for the monster.

local Players = game:GetService("Players"):GetPlayers()
local seeker = script.Parent
local pickedPlayer
game.Workspace.Active.Changed:Connect(function()
	if game.Workspace.Active.Value == true then
		wait(10)
	while game.Workspace.Active.Value == true do
			for _, player2 in ipairs(game.Players:GetPlayers()) do
		if player2.Character.Hiding.Value ~= true then
				seeker.Torso.CFrame = player2.Character.Torso.CFrame
				wait(2)
				if player2.Character.Hiding.Value ~= true then
					player2.Character.Humanoid.Health -= 100000000000
				end
		end
		end
		end
		end
end)

4 comments

0

What exactly do you mean with lag? every wait() causes the whole script to do nothing in that time. For ececuting too many times: If you dont set the acitve value to false again it is going to continue forever as you have a while loop.

0

wow there is a lot of spelling mistakes in my comment xD

0

The scripts exhausts its allowed execution time. I cant immediately disabled active as that's the value keeping the monster active.

0

did you just... "player2.Character.Humanoid.Health -= 100000000000"