- What does the code do and what are you not satisfied with?
So, I currently have a wizard game, and on one of the boss battles you have like a barrier, just like in fortnite. And I want to have a code, that both makes the barrier smaller and makes the plaeyer loose health when the player is outside the barrier.
So the problem is, that, it is working - most of the times, but like, every 10 or 20th time it fails. Often I get the error: script timeout exhausted allowed execution time. I believe it is because, I have a for loop inside a while loop. But, dear god. I am lost.
- What potential improvements have you considered?
Well, it must (I think) probably be something with the part where I constantly check if the player is outside the zone in order to take damage, there is something in that loop. - How (specifically) do you want to improve the code?
I basically want it to work bug free, right now. It is very hacky and doesnt work a lot of times, and I know for a fact that it can be done better. It is just out of my imagiantion so to speak. I just, want a simple âbattle royaleâ sort of thing, allthough that is not what I am making, it is kind of the same principle. Make the barrier smaller, and take damage if you are outside of that area. Yeah.
startBarrierShrink = game.Workspace.MainScripts.BarrierShrink.StartBarrierShrink
BARRIER = game.Workspace.Barrier
playersInGame = game.Workspace.PlayersInGame
serverStorage = game.ServerStorage
showRedScreenRemoteEvent = game.ReplicatedStorage.ShowRedScreen
function startBarrierShrink.OnInvoke(mapSpawned)
local damageLoopOn = true
centerOffMap = game.Workspace[mapSpawned].CenterOffMap
function makeSmaller1(startingPosition, endPosition)
for i = startingPosition,endPosition,-0.3 do
if #playersInGame:GetChildren() > 1 then
print("shrinking the boss battle barrier ")
BARRIER.Size = Vector3.new(i,308,i)
wait()
else
break
end
end
end
function makeSmaller2(startingPosition, endPosition)
for i = startingPosition,endPosition,-0.3 do
if #playersInGame:GetChildren() > 1 then
BARRIER.Size = Vector3.new(i,308,i)
wait()
else
break
end
end
end
function makeSmaller3(startingPosition, endPosition)
for i = startingPosition,endPosition,-0.3 do
if #playersInGame:GetChildren() > 1 then
BARRIER.Size = Vector3.new(i,308,i)
wait()
else
break
end
end
end
function makeSmaller4(startingPosition, endPosition)
for i = startingPosition,endPosition,-0.3 do
if #playersInGame:GetChildren() > 1 then
BARRIER.Size = Vector3.new(i,308,i)
wait()
else
break
end
end
end
function makeSmaller5(startingPosition, endPosition)
for i = startingPosition,endPosition,-0.3 do
if #playersInGame:GetChildren() > 1 then
BARRIER.Size = Vector3.new(i,308,i)
wait()
else
break
end
end
end
--[[
The while loop below runs in a coroutine so it doesnt interfere with the rest of the script
--]]
loopCheck = coroutine.create(function()
while damageLoopOn do
for i, stringValuePlayer in ipairs(game.Workspace.PlayersInGame:GetChildren()) do
player = game.Workspace:FindFirstChild(stringValuePlayer.Name)
if player then --innex nil with "HumanoidRootPart"
local hrp = player:WaitForChild("HumanoidRootPart")
local positionFromMiddle = ((hrp.Position - centerOffMap.Position) * Vector3.new(1, 0, 1)).magnitude
--playerGui = player:GetPlayerFromCharacter().PlayerGui DOESNT WORK
if positionFromMiddle > BARRIER.Size.X/2 and #playersInGame:GetChildren() > 1 then
--playerGui.OutsideOfZone.Enabled = true --DOESNT WORK
print(positionFromMiddle .. " position from middle")
print(BARRIER.Size.X/2 .. " barrier size")
local playerNotChar = game.Players:GetPlayerFromCharacter(player)
showRedScreenRemoteEvent:FireClient(playerNotChar)
player:WaitForChild("Humanoid"):TakeDamage(math.random(10.5555555555,12.55555555))
end
end
end
wait(1)
end
end)
coroutine.resume(loopCheck)
print(centerOffMap:GetFullName()) ---this is where the script timeout error thingy happens.
makeSmaller1(690,500)
wait(8)
makeSmaller2(500,420)
wait(math.random(20,25))
makeSmaller3(420, 350)
wait(15)
makeSmaller4(350, 280)
wait(math.random(10))
makeSmaller5(280, 1)
repeat
wait(1)
until #playersInGame:GetChildren() == 0
print("boss killed all wizard, or wizrds killed boss.")
--PROBABLY USE COROUTINE.YILED HERE OR SOMETHING!!
coroutine.yield(loopCheck)
damageLoopOn = false --makes sure the loop actually stops when no players.
BARRIER.Size = Vector3.new(690, 308, 690)
return
end
â
Guys, I will probably remake this post tomorrow. I just had to get it out, I have been sturggling with this script for 4 months. It will mean the world to me if someone could help. Thanks a lot lot lot.
(And yes, the script is awful. )