Hi,
I have a system, where 2 groups of 2 fans are activated (each groups is activated off-line)
The problem is when my player enter the region3, he get a bodyVelocity and i want to make it destroy when the player leave the region3 (here we use a other part named “regionEnded”).
The system work perfectly but just one times, after my player go in a fans and get the effect, the system is broken and my player will not get bodyVelocity any more (the first while true do (in the spawn()) keeps going.
Here is the code
-- << Services >> --
local TweenService = game:GetService("TweenService")
-- << Variables >> --
local FansFolder = game.Workspace.Levels.Level1.Traps.Fans
local debounceTraker = {}
local tween_Info = TweenInfo.new(
1.25,
Enum.EasingStyle.Linear,
Enum.EasingDirection.In,
3,
false,
0
)
local region3Zones_fans1_region = {}
local region3Zones_fans1_reference = {}
local region3Zones_fans2_region = {}
local region3Zones_fans2_reference = {}
local fans1_Table = {}
local fans2_Table = {}
local fans1_Activated
local fans2_Activated
-- << Functions >> --
local function applyVelocity(character, ActionZone)
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(math.huge,0,0)
BodyVelocity.P = math.huge
BodyVelocity.Velocity = (ActionZone.CFrame.RightVector *1)*35 --Change the velocity 35
BodyVelocity.Parent = character:FindFirstChild("HumanoidRootPart")
end
local function removeVelocity (character)
character.HumanoidRootPart.BodyVelocity:Destroy()
end
local function bladesRotation(center)
local tween_Result = TweenService:Create(center,tween_Info,{CFrame = center.CFrame * CFrame.Angles(0,math.rad(180),0)})
tween_Result:Play()
end
-- << Connect >> --
for i,v in pairs(FansFolder:GetChildren()) do
if v.Name == "Fan1" then
table.insert(fans1_Table,#fans1_Table+1,v)
elseif v.Name == "Fan2" then
table.insert(fans2_Table,#fans2_Table+1,v)
end
end
for i,v in pairs(fans1_Table) do
local reference = v.RegionDectectZone
local minRegion3 = reference.Position - (0.5 * reference.Size)
local maxRegion3 = reference.Position + (0.5 * reference.Size)
local region3 = Region3.new(minRegion3,maxRegion3)
table.insert(region3Zones_fans1_region,#region3Zones_fans1_region + 1, region3)
table.insert(region3Zones_fans1_reference,#region3Zones_fans1_reference + 1, reference)
end
for i,v in pairs(fans2_Table) do
local reference = v.RegionDectectZone
local minRegion3 = reference.Position - (0.5 * reference.Size)
local maxRegion3 = reference.Position + (0.5 * reference.Size)
local region3 = Region3.new(minRegion3,maxRegion3)
table.insert(region3Zones_fans2_region,#region3Zones_fans2_region + 1, region3)
table.insert(region3Zones_fans2_reference,#region3Zones_fans2_reference + 1, reference)
end
------- HERE IS THE .TouchEnded event for every part
for i, v in pairs(FansFolder:GetChildren()) do
v.regionEnded.TouchEnded:Connect(function(hit)
print("HIII")
if debounceTraker[hit.Parent] then
--print(debounceTraker[hit.Parent])
debounceTraker[hit.Parent] = nil
removeVelocity(hit.Parent)
--print(debounceTraker[hit.Parent])
end
end)
end
--
spawn(function()
while true do
fans1_Activated = true
fans2_Activated = false
local fansbuffer = {}
for i,v in pairs(fans1_Table) do
v.SoundEmitter.WindSound:Play()
-- Rotation System
local center = v.front.Blades.Center
bladesRotation(center)
-- Activate smoke
v.SmokePart.SmokeEmitter.Enabled = true
-- add element to the buffer
if #fansbuffer == 1 then
table.insert(fansbuffer,2,v)
else
table.insert(fansbuffer,1,v)
end
end
print("1 - Activated ; 2 - Desactivated")
wait(5)
fans1_Activated = false
fans2_Activated = true
for i,v in pairs(fans2_Table) do
-- remove element into the buffer and desactivate them
if #fansbuffer == 2 then
fansbuffer[1].SmokePart.SmokeEmitter.Enabled = false
fansbuffer[1].SoundEmitter.WindSound:Stop()
fansbuffer[2].SmokePart.SmokeEmitter.Enabled = false
fansbuffer[2].SoundEmitter.WindSound:Stop()
end
v.SoundEmitter.WindSound:Play()
-- Rotation System
local center = v.front.Blades.Center
bladesRotation(center)
-- Activate smoke
v.SmokePart.SmokeEmitter.Enabled = true
-- add element to the buffer
if #fansbuffer == 1 then
table.insert(fansbuffer,2,v)
else
table.insert(fansbuffer,1,v)
end
end
print("1 - Desactivated ; 2 - Activated")
wait(5)
-- remove element into the buffer and desactivate them
if #fansbuffer >= 2 then
fansbuffer[1].SmokePart.SmokeEmitter.Enabled = false
fansbuffer[1].SoundEmitter.WindSound:Stop()
fansbuffer[2].SmokePart.SmokeEmitter.Enabled = false
fansbuffer[2].SoundEmitter.WindSound:Stop()
end
end
end)
--Here is the while true do that break after the player run and get in the region for the 1st time.
while true do
--print("Why?")
if fans1_Activated == true then
for i, v in pairs(region3Zones_fans1_region) do
local currRegion = v
local currReference = region3Zones_fans1_reference[i]
local result = workspace:FindPartsInRegion3WithIgnoreList(currRegion, {currReference}, math.huge)
for i,v in ipairs(result) do
local char = v.Parent
if debounceTraker[char] then
return
end
if char and char:FindFirstChild("Humanoid") and fans1_Activated == true then
debounceTraker[char] = true
print("fan1Touched")
applyVelocity(char,currReference)
--[[
wait(1)--reduire time
debounceTraker[char] = nil
removeVelocity(char,currReference)
print("remove")
]]--
end
end
end
elseif fans2_Activated == true then
for i, v in pairs(region3Zones_fans2_region) do
local currRegion = v
local currReference = region3Zones_fans2_reference[i]
local result = workspace:FindPartsInRegion3WithIgnoreList(currRegion, {currReference}, math.huge)
for i,v in ipairs(result) do
local char = v.Parent
if debounceTraker[char] then
return
end
if char and char:FindFirstChild("Humanoid") and fans2_Activated == true then
debounceTraker[char] = true
print("fan2Touched")
applyVelocity(char,currReference)
--[[
wait(1) --reduire time
debounceTraker[char] = nil
removeVelocity(char,currReference)
print("remove")
]]--
end
end
end
end
wait()
end
Thanks.