#piece of code running unprovoked>

1 messages · Page 1 of 1 (latest)

inner jasperBOT
#

studio** You are now Level 6! **studio

fair coyote
#
local storage = script.Parent.Parent.current
local inzone = script.Parent.Inzone
local zone = script.Parent

zone.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if hit.Parent:FindFirstChild("Humanoid") then
            inzone.Value = true
        end
    end
end)

zone.TouchEnded:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if hit.Parent:FindFirstChild("Humanoid") then
            inzone.Value = false
        end
    end
end)

while task.wait() do
    local enemynum = #storage:GetChildren()
    
    if inzone.Value == true and enemynum < 5 then
        local enemy = game.ServerStorage.enemy:Clone()
        
        enemy.Parent = storage
        print("there is " ..enemynum.. " enemies!")
        task.wait(1)
    elseif inzone.Value == false and enemynum > 0 then
        print("Cleared "..enemynum.." enemies!")
        storage:ClearAllChildren()
    end
end
#

This is the main spawning lines

while task.wait() do
    local enemynum = #storage:GetChildren()
    
    if inzone.Value == true and enemynum < 5 then
        local enemy = game.ServerStorage.enemy:Clone()
        
        enemy.Parent = storage
        print("there is " ..enemynum.. " enemies!")
        task.wait(1)
    elseif inzone.Value == false and enemynum > 0 then
        print("Cleared "..enemynum.." enemies!")
        storage:ClearAllChildren()
    end
end
quasi heath
#

I'm imagining that the NPCs all manage to trigger TouchEnded at the same time, causing the "InZone" flag to be put down

#

There is a better way to approach this. A way that does not struggle with this issue

#

Use Zone.playerEntered and Zone.playerExited to learn when the player enters and leaves the zone

fair coyote
fair coyote
fair coyote
fair coyote
quasi heath
#

Oh, lmfao

#

Sorry. I've slept poorly the past couple days. I thought you already were using this technology

fair coyote
fair coyote
# quasi heath Sorry. I've slept poorly the past couple days. I thought you already were using ...

So i fiddled with it, and it kinda works but now the delay between each enemy spawn is now gone unlike before

local field = workspace.field
local storage = field.current
local replicatedStorage = game:GetService("ReplicatedStorage")
local zonePlus = require(replicatedStorage.Zone)

local spawnZone = field.zone

local zone1 = zonePlus.new(spawnZone)

while task.wait(1) do
    zone1.playerEntered:Connect(function(player: Player)
        local enemynum = #storage:GetChildren()

        if enemynum < 5 then
            local enemy = game.ServerStorage.enemy:Clone()

            enemy.Parent = storage
            task.wait(1)
            print("there is " ..enemynum.. " enemies!")
        end    
    end)

    zone1.playerExited:Connect(function(player: Player)
        local enemynum = #storage:GetChildren()
        print("Cleared "..enemynum.." enemies!")
        storage:ClearAllChildren()
    end)
end
cloud chasm
#

why are you making connections inside while loop

fair coyote
cloud chasm
#

it works but will overwrite everything since you're creating 1000 connection per second

#

move it outside

#

same as the old code but with the ZonePlus

#

instead of roblox's touched and touchended

fair coyote
#

but wont i have to use 2 loops?

cloud chasm
#

no

#

what made you think you need another loop

fair coyote
#

well because i need to loop the playerEntered and playerExited no?

cloud chasm
#

no

fair coyote
#

?

cloud chasm
#

ZonePlus already have it handled internally

#

you jsut need to connect to that playerentered/exited event

fair coyote
cloud chasm
#

replace zone with ZonePlus's object

fair coyote
#

pardon me if I sound oblivious, I've never even touched or heard of ZonePlus

fair coyote
#

thats what i have here right?

local field = workspace.field
local storage = field.current
local replicatedStorage = game:GetService("ReplicatedStorage")
local zonePlus = require(replicatedStorage.Zone)

local spawnZone = field.zone

local zone1 = zonePlus.new(spawnZone)

zone1.playerEntered:Connect(function(player: Player)
    local enemynum = #storage:GetChildren()

    if enemynum < 5 then
        local enemy = game.ServerStorage.enemy:Clone()

        enemy.Parent = storage
        task.wait(1)
        print("there is " ..enemynum.. " enemies!")
    end    
end)

zone1.playerExited:Connect(function(player: Player)
    local enemynum = #storage:GetChildren()

    print("Cleared "..enemynum.." enemies!")
    storage:ClearAllChildren()
end)
cloud chasm
#
local field = workspace.field
local storage = field.current
local replicatedStorage = game:GetService("ReplicatedStorage")
local zonePlus = require(replicatedStorage.Zone)

local spawnZone = field.zone

local zone1 = zonePlus.new(spawnZone)

zone1.playerEntered:Connect(function(player: Player)
    inzone.Value = true
end)

zone1.playerExited:Connect(function(player: Player)
    inzone.Value = false
end)

while true do
    local enemynum = #storage:GetChildren()
    
    if inzone.Value == true and enemynum < 5 then
        local enemy = game.ServerStorage.enemy:Clone()
        
        enemy.Parent = storage
        print("there is " ..enemynum.. " enemies!")
        task.wait(1)

    elseif inzone.Value == false and enemynum > 0 then
        print("Cleared "..enemynum.." enemies!")
        storage:ClearAllChildren()
    end
end
fair coyote
fair coyote
cloud chasm
fair coyote
quasi heath
#
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage     = game:GetService("ServerStorage")


local ZonePlus = require(ReplicatedStorage.ZonePlus)

local Field   = workspace.Field
local Enemies = Field.Enemies
local Enemy   = ServerStorage.Enemy


local MAX_ENEMIES      = 5
local ENEMY_SPAWN_RATE = 1

local enemyZone         = ZonePlus.new(Field.Zone)
local enemySpawnProcess = nil :: thread?



local function spawnEnemies()
    for _ = 1, MAX_ENEMIES do
        Enemy:Clone().Parent = Enemies
        
        task.wait(ENEMY_SPAWN_RATE)
    end
end

local function onPlayerEntered()
    if not enemySpawnProcess then
        enemySpawnProcess = task.defer(spawnEnemies)
    end
end

local function onPlayerExited()
    if #enemyZone:getPlayers() > 0 then
        return
    end
    
    task.cancel(enemySpawnProcess :: thread)
    enemySpawnProcess = nil
    
    Enemies:ClearAllChildren()
end



enemyZone.playerEntered:Connect(onPlayerEntered)
enemyZone.playerExited:Connect(onPlayerExited)