Hello here is the problem. I have a script that check if the player is ont a part (cylinder) and if the “engine” is on. If it’s on then the player is push away… The problem here is that when the player don’t move, and the engine is turn off, he can stand in the area, but when the engine turn on again, he problably don’t detect any player (if he don’t move)…
I Hope you will understand.
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 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
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
v.DetectZone.Touched:Connect(function(hit)
if debounceTraker[hit.Parent] then
return
end
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") and fans1_Activated == true then
local character = hit.Parent
debounceTraker[character] = true
applyVelocity(character,v.DetectZone)
end
end)
v.DetectZone.TouchEnded:Connect(function(hit)
if debounceTraker[hit.Parent] then
debounceTraker[hit.Parent] = nil
removeVelocity(hit.Parent)
end
end)
end
for i,v in pairs(fans2_Table) do
v.DetectZone.Touched:Connect(function(hit)
if debounceTraker[hit.Parent] then
return
end
if hit.Parent and hit.Parent:FindFirstChild("Humanoid") and fans2_Activated == true then
local character = hit.Parent
debounceTraker[character] = true
applyVelocity(character,v.DetectZone)
end
end)
v.DetectZone.TouchEnded:Connect(function(hit)
if debounceTraker[hit.Parent] then
debounceTraker[hit.Parent] = nil
removeVelocity(hit.Parent)
end
end)
end
while true do
fans1_Activated = true
fans2_Activated = false
local fansbuffer = {}
for i,v in pairs(fans1_Table) do
-- 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[2].SmokePart.SmokeEmitter.Enabled = false
end
-- 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
wait(5)
-- remove element into the buffer and desactivate them
if #fansbuffer >= 2 then
fansbuffer[1].SmokePart.SmokeEmitter.Enabled = false
fansbuffer[2].SmokePart.SmokeEmitter.Enabled = false
end
--print("1 - Desactivated ; 2 - Activated")
end
I’m pretty sure it’s because of the Touched event that doesn’t “refresh” and he don’t detect the character before he move again.
Thanks you very much.