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

Plane touched event not connecting?

Asked by 2 years ago
Edited 2 years ago

I'm currently working on a game where you can fly a plane. I'm currently trying to implement a system where if the plane collides with a part it checks the speed and if it was going over a certain speed it explodes. I'm controlling the plain using UserInputService and moving it with CFrames. This is all being done in a Local Script. The plane object is a meshPart and planeHitBox is a basePart.

The problem is when I run the code even after colliding with things it doesn't trigger an event, i.e. the print statement doesn't run inside the function. Here's my code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")

local plane = workspace.Plane
local startFrame = plane.CFrame

local roll = CFrame.Angles(0,0,0)
local pitch = CFrame.Angles(0,0,0)
local yaw = CFrame.Angles(0,0,0)
local throttle = CFrame.new(0,0,-0.5)

--Throttle Controls
local THROTTLE_SPEED = 0.08
local MAX_THROTTLE = 3
local THROTTLE_DECLERATION = 0.02

--Other Controls
local ROLL_SPEED = 0.1
local PITCH_SPEED = 0.05
local YAW_SPEED = 0.02

--Physic Values
local DRAG = 0.05

local isInput = false

local function breakPlane()
    roll = CFrame.Angles(0,0,0)
    pitch = CFrame.Angles(0,0,0)
    yaw = CFrame.Angles(0,0,0)
    throttle = CFrame.new(0,0,0)
    plane.Anchored = true
    plane.Fire.Enabled = true
    plane.Explosion.Position = plane.Position
    plane.Explosion.Visible = true
end

ReplicatedStorage.switchView.Event:Connect(function()
    local camera = workspace.CurrentCamera
    print("Fired")

    plane.planeHitBox.Touched:Connect(function()
        print("collison")
        local throtPos = throttle.Position
        print(throtPos)
        if throtPos.X > 1 or throtPos.Y > 1 or throtPos.Z >1 then
            breakPlane()
        end
    end)

    UIS.InputEnded:Connect(function()
        isInput = false
        roll = CFrame.Angles(0,0,0)
        pitch = CFrame.Angles(0,0,0)
        yaw = CFrame.Angles(0,0,0)
    end)

    UIS.InputBegan:Connect(function(input)
        local key = input.KeyCode
        local keyC = Enum.KeyCode
        isInput = true
        repeat
            if key == keyC.A then
                --Yaw Left
                yaw *= CFrame.Angles(0,math.rad(YAW_SPEED),0)
            elseif key == keyC.D then
                --Yaw Right
                yaw *= CFrame.Angles(0,math.rad(-YAW_SPEED),0,0)
            elseif key == keyC.W then
                --Pitch Down
                pitch *= CFrame.Angles(math.rad(-PITCH_SPEED),0,0)
            elseif key == keyC.S then
                --Pitch Up
                pitch *= CFrame.Angles(math.rad(PITCH_SPEED),0,0)
            elseif key == keyC.E then
                --Roll Right
                roll *= CFrame.Angles(0,0,math.rad(-ROLL_SPEED))
            elseif key == keyC.Q then
                --Roll Left
                roll *= CFrame.Angles(0,0,math.rad(ROLL_SPEED))
            elseif key == keyC.R then
                --Increase Throttle
                if throttle.Position.Z > -MAX_THROTTLE then
                    throttle *= CFrame.new(0,0,-THROTTLE_SPEED)
                end
            elseif key == keyC.F then
                --Decrease Throttle
                if throttle.Position.Z <= -0.5 then
                    throttle *= CFrame.new(0,0,THROTTLE_DECLERATION)
                end
            end
            wait()
        until isInput == false
    end)

    while true do
        wait()
        camera.CFrame = plane.planeCamera.CFrame
        plane.CFrame = plane.CFrame * throttle * pitch * roll * yaw * CFrame.new(0,0,DRAG)
    end
end)

All help is appreciated thank you! If you have any questions just comment on this and I'll respond.

0
Touched event is quite wank while managed by the client, sometimes it doesn't fire at all, sometimes it fires too early, etc RAFA1608 543 — 2y
0
By the way, I thought .Event was only for BindableEvents? RAFA1608 543 — 2y

Answer this question