Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script stops running after a few seconds? Even with while true do?

Asked by 4 years ago

Alright so I'm trying to make a payload, and I just recently learned about Region3. The question I have is that the payload stops moving (after a couple of seconds) even when I'm inside the "Region / PayloadZone", If I leave the PayloadZone and come back, it still doesn't move. It's in a while true do loop so it shouldn't be stopping. There's no error messages, and it displays that it's still searching for parts.

local payload = game.Workspace.Payload
local payloadZone = game.Workspace.PayloadZone
local inZone = game.Workspace.InnerZone
local payloadBVel = payload.BodyVelocity
local inZoneBVel = inZone.BodyVelocity
local pos1, pos2 = (payloadZone.Position - (payloadZone.Size / 2)), (payloadZone.Position + (payloadZone.Size / 2))
local region = Region3.new(pos1, pos2)

while true do
    wait(1)
    payloadBVel.Velocity = Vector3.new(0,0,0)
    inZoneBVel.Velocity = Vector3.new(0,0,0)
    payloadZone.Position = inZone.Position
    print ("Searching for parts to be in the Zone")
    local partsInRegion = workspace:FindPartsInRegion3(region, game.Workspace.Terrain, 50)
    local playersFound = {} --Table of players found in Region
    for i, part in pairs (partsInRegion) do
        if part.Parent:FindFirstChild("Humanoid") ~= nil then
            print (part.Parent.Name, "Found in the Zone!")
            playersFound[part.Parent.Name] = part.Parent --Add Player's Character to Table
        end
    end
    --Effect of being in the Zone
    for plrName, char in pairs (playersFound) do
        payloadBVel.Velocity = Vector3.new(-3,0,0)
        inZoneBVel.Velocity = Vector3.new(-3,0,0)
    end
end

1 answer

Log in to vote
0
Answered by 4 years ago

The problem is that you define the region outside the while loop meaning the region will never update and will always stay at the exact same position. Therefore when the payload moves it can never detect someone is nearby again cause the region is at the wrong position.

Ad

Answer this question