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

My dashing script is not working even after i tried raycasting, what do i do now?

Asked by 1 year ago

So, i recently started making the combat system for my game. First thing i made was the "dashing system" (not really dashing, but the idea behind it is that every time the player presses E, they teleport a few studs forward with any NPCs that are infront of them before the teleport get knocked up and stunned)

Well, first problem i ran into was that the player can teleport not only through walls, but if they are too chonky, they can teleport inside them.

Someone recommended that i use raycasting, which i learned and used in the script below, which of course, still has problems

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local deb = false

player.CharacterAdded:Connect(function(char)

    local char = player.Character
    local Hum = char:WaitForChild("Humanoid")
    local HRP = char:WaitForChild("HumanoidRootPart")

    UIS.InputBegan:Connect(function(input, gpe)

        if gpe then return end

        local RayPos = HRP.Position
        local RayDirection = HRP.CFrame.LookVector * 50
        local RayParams = RaycastParams.new()
        RayParams.FilterDescendantsInstances = {

            game.Workspace["Drooling Zombie"],
            game.Workspace.Baseplate

        }

        RayParams.FilterType = Enum.RaycastFilterType.Blacklist
        RayParams.IgnoreWater = true

        if input.KeyCode == Enum.KeyCode.E and deb == false then

            local ray = workspace:Raycast(RayPos, RayDirection, RayParams)

            if ray then
                local collision = ray.Position

                if collision then
                    deb = true

                    Hum.WalkSpeed = 0
                    Hum.JumpPower = 0
                    HRP.Anchored = true

                    task.wait(1)

                    HRP.Position = collision

                    task.wait(0.25)

                    Hum.WalkSpeed = 16
                    Hum.JumpPower = 50
                    HRP.Anchored = false

                    deb = false                 
                elseif not collision then
                    deb = true

                    Hum.WalkSpeed = 0
                    Hum.JumpPower = 0
                    HRP.Anchored = true

                    task.wait(1)

                    HRP.CFrame = HRP.CFrame + (HRP.CFrame.LookVector * 50)

                    task.wait(0.25)

                    Hum.WalkSpeed = 16
                    Hum.JumpPower = 50
                    HRP.Anchored = false

                    deb = false
                end
            end         
        end
    end)
end)

Well, the problem with it is that there are no errors, but, when the ray has collision the HumanoidRootPart gets teleported and it makes the camera all janky because it leaves the rest of the player behind.

Also, even if there are no errors, if the ray has no collision, then the script flat out does not run, at all, even if i have an Elseif statement inside that clearly states that "if there's no collision just do this"

I really need help with this one homies. I appreciate any of it.

1 answer

Log in to vote
0
Answered by 1 year ago

You're not checking collision at all, you're just checking if ray.Position exists, which it does. To check if the intersection has collision, you get the part the ray intersected at and check its collision by checking if the CanCollide property is true or false.

To avoid HumanoidRootPart leaving the rest of the limbs, use PivotTo().

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local deb = false

UIS.InputBegan:Connect(function(input, gpe)
    if gpe then return end

        if input.KeyCode == Enum.KeyCode.E and deb == false then
        local char = player.Character
        if not char or not char.Parent then
            char = player.CharacterAdded:Wait()
        end

        local Hum = char:WaitForChild("Humanoid")
        local HRP = char:WaitForChild("HumanoidRootPart")

        local RayPos = HRP.Position
        local RayDirection = char:GetPivot().LookVector * 50
        local RayParams = RaycastParams.new()
        RayParams.FilterDescendantsInstances = {
            workspace:WaitForChild("Drooling Zombie"),
            workspace:WaitForChild("Baseplate")
        }

        RayParams.FilterType = Enum.RaycastFilterType.Blacklist
        RayParams.IgnoreWater = true

        local ray = workspace:Raycast(RayPos, RayDirection, RayParams)
        if ray then -- if `ray` intersected at a part (if not, will return nil)
            local hit = ray.Instance
            local pos = ray.Position

            if hit.CanCollide then -- if `hit` is collidable
                deb = true

                Hum.WalkSpeed = 0
                Hum.JumpPower = 0
                HRP.Anchored = true

                task.wait(1)
                char:PivotTo(CFrame.new(pos))

                task.wait(0.25)
                Hum.WalkSpeed = 16
                Hum.JumpPower = 50
                HRP.Anchored = false

                deb = false
                return -- stops the entire function, preventing the rest of the code to run
            end       
        end
        -- this part of the code will run if `ray` didn't intersect at any part (nil), or if `hit` is non collidable
        deb = true

        Hum.WalkSpeed = 0
        Hum.JumpPower = 0
        HRP.Anchored = true

        task.wait(1)
        char:PivotTo(char:GetPivot() + RayDirection)

        task.wait(0.25)
        Hum.WalkSpeed = 16
        Hum.JumpPower = 50
        HRP.Anchored = false

        deb = false
    end
end)
0
Thank you, it worked. Man i feel like an idiot now, but either way, this was really helpful. I appreciate it lolmarios2647 46 — 1y
Ad

Answer this question