I have a little script that handles a spider-web like function that damages player every 1 second and usually has an if statements to break the loop, it works great but i’m just wondering if it’s better off to use a repeat until or a even better a greater approach?
workspace:FindFirstChild("spider web").Touched:Connect(function(__PLAYER)
local __CHARACTERPLAYER = Players:GetPlayerFromCharacter(__PLAYER.Parent)
if __CHARACTERPLAYER ~= nil then
local __CHARACTER = __CHARACTERPLAYER.Character
local __HIM = __CHARACTER:FindFirstChildOfClass("Humanoid")
local __OCCUPIED = workspace:FindFirstChild("spider web"):FindFirstChild("__OCCUPIED").Value -- Boolean to check if somebody inside the spider web
local __ESCAPED = ReplicatedStorage:FindFirstChild(__CHARACTERPLAYER.Name):WaitForChild("__ESCAPED").Value -- Boolean to check if player recently escaped.
local __ESCAPEDTIME = ReplicatedStorage:FindFirstChild(__CHARACTERPLAYER.Name):WaitForChild("__ESCAPEDTIME") -- NumberValue that goes from 20 to 0 to make the player escape
if __OCCUPIED == false then
__ESCAPEDTIME.Value = 20 -- force value to be 20 incase a player ever gets stuck again.
__OCCUPIED = true
while true do
if __OCCUPIED == true then
if __ESCAPED == false then
task.wait(1)
__CHARACTER.HumanoidRootPart.Anchored = true -- Replicate stunning
__HIM:TakeDamage(10) -- Damaging player every 1 second
if __ESCAPEDTIME.Value <= 0 then -- Player escaped inside spider web
--__ESCAPETIME value is changed through a server invoke.
workspace:FindFirstChild("spider web"):Destroy()
__ESCAPED = true
__CHARACTER.HumanoidRootPart.Anchored = false
local BodyVelocity = Instance.new("BodyVelocity", __CHARACTER.Torso)
ReplicatedStorage:FindFirstChild(__CHARACTERPLAYER.Name):WaitForChild("__EVENTS"):WaitForChild("__RAGDOLL"):Fire(1.2)
BodyVelocity.Velocity = Vector3.new(0,10,0) * 15
game.Debris:AddItem(BodyVelocity, 1)
break -- Did they escape? Nope! continue damaging
end
if __HIM.Health <= 10 then -- Player has died inside spider web
__HIM.Health = 100
workspace:FindFirstChild("spider web"):Destroy()
__ESCAPED = true
__ESCAPEDTIME.Value = 20
__CHARACTER.HumanoidRootPart.Anchored = false
__OCCUPIED = false
break
else -- did they die yet?
continue -- nope! continue looping
end
end
end
end
end
end
end)