#piece of code running unprovoked>
1 messages · Page 1 of 1 (latest)
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
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
ill definitely be trying this out, though I will ask, how is it the NPCs if they never leave the zone either?
wait, what are these terms? They arent showing up
like this? Because this isnt printing anything
local zone = script.Parent
zone.playerEntered:Connect(function(player)
print(player.. "touched zone")
end)
oh nevermind i see i need the plugin thing
Oh, lmfao
Sorry. I've slept poorly the past couple days. I thought you already were using this technology
all good, this is really my first time trying to make an actual game and scripting it by myself
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
why are you making connections inside while loop
does that not work? I just need it to constantly know someone is in the zone or not
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
plus memory leak
but wont i have to use 2 loops?
well because i need to loop the playerEntered and playerExited no?
no
?
ZonePlus already have it handled internally
you jsut need to connect to that playerentered/exited event
wait im n=still not very good at arranging script. How do I put the connects on the outside of the loop while letting each function of them connect to their respective piece?
^^
replace zone with ZonePlus's object
i did, but that script was before it was in ReplicatedStorage, so "field.zone" is the zone, and "zone1" is ZonePlus's objest connected to it if im understanding u right
pardon me if I sound oblivious, I've never even touched or heard of ZonePlus
yes
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)
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
oh i feel stupid now. I was also watching a video on zoneplus and the part where they put the extra logic in ZonePlus's playerEntered/Exited's function threw me off thinking thats everything that happens upon touch
got it working! Thanks so much! I had gotten rid of alot of my old code after working with zoneplus so i didnt even think to go back
did you even take a look at their documentation?
a little bit, I was trying to read and implement as I go
This code has the potential to crash, and will not work in multiplayer
--!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)
** You are now Level 6! **