Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

Write a Roblox dashing system where when a player double taps W, A, S, or D in a certain…

  1. User

    Write a Roblox dashing system where when a player double taps W, A, S, or D in a certain timeframe, then the player's root part has a body velocity added into the direction they went. The dash should be relative to the camera, so if the player is facing backward but the camera is facing forward and the user double taps A, then the player would dash left relative to the camera but technically dashing right relative to the player.

    It should also be compatible with other keys at the same time. If the player double taps both W and A or W and D and etc, then they can go forward and right or forward and left or backward and right and etc.

    If two of the opposite keys are pressed, like A and D, then the player just doesn't attempt to dash, it needs to be a working pair, so no W and S or A and D.

    Play an animation and rotate the player towards the direction they are dashing so they are facing their movement. When they finish dashing, rotate the player back to how they were when they first dashed

    Add a blank function for when the player dashes, when they player stops dashing, when the player's dash is interrupted, and when the player hits another player while dashing.

    If the player dashes into a wall or an object that is anchored and has cancollide on and is bigger than the players root part, then the player plays another animation, does not rotate back to where they were when they dashed, and has a longer cooldown of dashing.

    The player hitting a wall is considered an interruption.

    If the player runs into another player while dashing, then the other player is ragdolled (using the "RagdollTrigger" inside of every player's character model), and a body velocity is added that pushes the player in the direction the player was dashing and damage the player 25% of their health. Uncheck the RagdollTrigger bool value after 2.5 seconds of being hit by a dashing player.

    The player dashing after hitting a player continues dashing, unless of course they hit a wall.

    Can you do this?

  2. Assistant

    Yes, I can help you create a Roblox dashing system that meets your specifications. Below is the implementation of the system using Lua scripting in Roblox. The system consists of a LocalScript that handles the player input and movement, and a ServerScript that handles interactions like damaging other players and ragdolling them upon collision.

    Overview

    1. LocalScript: Placed inside StarterPlayerScripts, it handles input detection, dashing logic, and communicates with the server when necessary.
    2. ServerScript: Placed in ServerScriptService, it handles server-side logic like applying damage and ragdoll effects to other players.

    LocalScript (PlayerDashScript)

    Place this script inside StarterPlayer > StarterPlayerScripts.

    -- PlayerDashScript
    local Player = game.Players.LocalPlayer
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local Humanoid = Character:WaitForChild("Humanoid")
    local RootPart = Character:WaitForChild("HumanoidRootPart")
    local UserInputService = game:GetService("UserInputService")
    local RunService = game:GetService("RunService")
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    
    -- RemoteEvent to communicate with server
    local DashEvent = ReplicatedStorage:WaitForChild("DashEvent")
    
    -- Dash Configuration
    local DASH_SPEED = 80
    local DASH_DURATION = 0.3
    local DASH_COOLDOWN = 1
    local DOUBLE_TAP_TIME = 0.3
    local WALL_HIT_COOLDOWN = 2  -- Longer cooldown when hitting a wall
    
    -- Variables
    local lastKeyPressTimes = {}
    local canDash = true
    local isDashing = false
    local dashStartTime = 0
    local dashDirection = Vector3.new()
    local originalOrientation = nil
    
    -- Functions for dash events
    local function onDashStart()
        -- Blank function to override
    end
    
    local function onDashEnd()
        -- Blank function to override
    end
    
    local function onDashInterrupted()
        -- Blank function to override
    end
    
    local function onPlayerHit(hitPlayer)
        -- Blank function to override
    end
    
    -- Detect double-tap input
    UserInputService.InputBegan:Connect(function(input, processed)
        if processed or not canDash or isDashing then return end
        
        if input.UserInputType == Enum.UserInputType.Keyboard then
            local key = input.KeyCode
            if key == Enum.KeyCode.W or key == Enum.KeyCode.A or key == Enum.KeyCode.S or key == Enum.KeyCode.D then
                local currentTime = tick()
                if lastKeyPressTimes[key] and currentTime - lastKeyPressTimes[key] <= DOUBLE_TAP_TIME then
                    -- Double-tap detected
                    local camera = workspace.CurrentCamera
                    local camCFrame = camera.CFrame
                    local moveDirection = Vector3.new()
                    
                    -- Get movement directions
                    if key == Enum.KeyCode.W then
                        moveDirection = moveDirection + camCFrame.LookVector * Vector3.new(1, 0, 1)
                    elseif key == Enum.KeyCode.S then
                        moveDirection = moveDirection - camCFrame.LookVector * Vector3.new(1, 0, 1)
                    elseif key == Enum.KeyCode.A then
                        moveDirection = moveDirection - camCFrame.RightVector * Vector3.new(1, 0, 1)
                    elseif key == Enum.KeyCode.D then
                        moveDirection = moveDirection + camCFrame.RightVector * Vector3.new(1, 0, 1)
                    end
                    
                    -- Normalize direction
                    if moveDirection.Magnitude > 0 then
                        -- Check for opposite keys
                        if UserInputService:IsKeyDown(Enum.KeyCode.W) and UserInputService:IsKeyDown(Enum.KeyCode.S) then return end
                        if UserInputService:IsKeyDown(Enum.KeyCode.A) and UserInputService:IsKeyDown(Enum.KeyCode.D) then return end
                        
                        dashDirection = moveDirection.Unit
                        startDash()
                    end
                else
                    lastKeyPressTimes[key] = currentTime
                end
            end
        end
    end)
    
    function startDash()
        isDashing = true
        canDash = false
        dashStartTime = tick()
        originalOrientation = RootPart.CFrame
        onDashStart()
        
        -- Play dash animation
        local dashAnimation = Instance.new("Animation")
        dashAnimation.AnimationId = "rbxassetid://YOUR_DASH_ANIMATION_ID"
        local dashTrack = Humanoid:LoadAnimation(dashAnimation)
        dashTrack:Play()
        
        -- Rotate player towards dash direction
        local lookCFrame = CFrame.new(RootPart.Position, RootPart.Position + Vector3.new(dashDirection.X, 0, dashDirection.Z))
        RootPart.CFrame = lookCFrame
    
        -- Create BodyVelocity
        local bodyVelocity = Instance.new("BodyVelocity")
        bodyVelocity.MaxForce = Vector3.new(1e5, 0, 1e5)
        bodyVelocity.Velocity = dashDirection * DASH_SPEED
        bodyVelocity.Parent = RootPart
    
        -- Dash duration
        wait(DASH_DURATION)
        
        -- Clean up
        bodyVelocity:Destroy()
        isDashing = false
        onDashEnd()
        
        -- Rotate back to original orientation
        RootPart.CFrame = originalOrientation
        
        -- Cooldown
        wait(DASH_COOLDOWN)
        canDash = true
    end
    
    -- Collision detection
    local function onTouched(otherPart)
        if not isDashing then return end
        local otherHumanoid = otherPart.Parent:FindFirstChild("Humanoid")
        if otherHumanoid and otherHumanoid ~= Humanoid then
            -- Hit another player
            onPlayerHit(otherHumanoid.Parent)
            DashEvent:FireServer("PlayerHit", otherHumanoid.Parent, dashDirection)
        elseif otherPart.CanCollide and otherPart.Anchored and otherPart.Size.Magnitude > RootPart.Size.Magnitude then
            -- Hit a wall or object
            isDashing = false
            onDashInterrupted()
            
            -- Play interruption animation
            local interruptAnimation = Instance.new("Animation")
            interruptAnimation.AnimationId = "rbxassetid://YOUR_INTERRUPT_ANIMATION_ID"
            local interruptTrack = Humanoid:LoadAnimation(interruptAnimation)
            interruptTrack:Play()
            
            -- Longer cooldown
            wait(WALL_HIT_COOLDOWN)
            canDash = true
        end
    end
    
    -- Connect collision detection
    RootPart.Touched:Connect(onTouched)
    

    Notes:

    • Replace "rbxassetid://YOUR_DASH_ANIMATION_ID" and "rbxassetid://YOUR_INTERRUPT_ANIMATION_ID" with the actual asset IDs of your animations.
    • The onDashStart, onDashEnd, onDashInterrupted, and onPlayerHit functions are provided as blank functions for you to customize as needed.
    • The DashEvent is a RemoteEvent used to communicate with the server script.

    ServerScript (DashServerScript)

    Place this script inside ServerScriptService.

    -- DashServerScript
    local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local DashEvent = ReplicatedStorage:WaitForChild("DashEvent")
    
    -- Functions for dash events
    local function onDashStart(player)
        -- Blank function to override
    end
    
    local function onDashEnd(player)
        -- Blank function to override
    end
    
    local function onDashInterrupted(player)
        -- Blank function to override
    end
    
    local function onPlayerHit(player, hitPlayer)
        -- Blank function to override
    end
    
    DashEvent.OnServerEvent:Connect(function(player, action, target, dashDirection)
        if action == "PlayerHit" and target and dashDirection then
            local humanoid = target:FindFirstChild("Humanoid")
            if humanoid then
                -- Damage the player (25% of health)
                local damage = humanoid.MaxHealth * 0.25
                humanoid:TakeDamage(damage)
                
                -- Apply ragdoll
                local ragdollTrigger = target:FindFirstChild("RagdollTrigger")
                if ragdollTrigger and ragdollTrigger:IsA("BoolValue") then
                    ragdollTrigger.Value = true
                end
                
                -- Apply push force
                local rootPart = target:FindFirstChild("HumanoidRootPart")
                if rootPart then
                    local bodyVelocity = Instance.new("BodyVelocity")
                    bodyVelocity.MaxForce = Vector3.new(1e5, 0, 1e5)
                    bodyVelocity.Velocity = dashDirection * 50
                    bodyVelocity.Parent = rootPart
                    
                    -- Remove after a short duration
                    game.Debris:AddItem(bodyVelocity, 0.5)
                end
                
                -- Uncheck RagdollTrigger after 2.5 seconds
                delay(2.5, function()
                    if ragdollTrigger then
                        ragdollTrigger.Value = false
                    end
                end)
                
                onPlayerHit(player, target)
            end
        end
    end)
    

    Notes:

    • The server script listens for events from the client to apply damage and ragdoll effects.
    • The onDashStart, onDashEnd, onDashInterrupted, and onPlayerHit functions are provided as blank functions for customization.

    Setting Up the RemoteEvent

    To enable communication between the client and server scripts, you need to add a RemoteEvent named DashEvent inside ReplicatedStorage.

    1. Create the RemoteEvent:

      • In ReplicatedStorage, add a new RemoteEvent.
      • Name it DashEvent.

    Additional Setup

    • Animations:

      • Create or upload your dash and interruption animations.
      • Make sure to replace the animation asset IDs in the scripts with your own.
    • Ragdoll System:

      • Each player's character model should contain a BoolValue named RagdollTrigger.
      • You need to implement a ragdoll system that listens to changes in RagdollTrigger and applies the ragdoll effect accordingly.

    Explanation

    • Input Detection:

      • The LocalScript uses UserInputService to detect keyboard inputs.
      • It checks for double-taps on movement keys within DOUBLE_TAP_TIME.
    • Dash Logic:

      • When a double-tap is detected, it calculates the dash direction relative to the camera.
      • It ensures that opposite keys are not pressed simultaneously.
      • It starts the dash by adding a BodyVelocity to the HumanoidRootPart.
      • It rotates the player to face the dash direction and plays a dash animation.
      • After the dash duration, it stops the dash and resets orientation.
    • Collision Detection:

      • The onTouched function detects collisions during a dash.
      • If the player hits another player, it informs the server to apply damage and ragdoll.
      • If the player hits a wall, it interrupts the dash, plays an interruption animation, and applies a longer cooldown.
    • Server-Side Handling:

      • The ServerScript handles applying damage and ragdoll effects to the hit player.
      • It ensures that only valid actions are performed on the server for security.

    Customization

    • Animation IDs:

      • Replace the placeholder animation IDs with your own.
    • Dash Speed and Duration:

      • Adjust DASH_SPEED and DASH_DURATION to change how the dash feels.
    • Dash Cooldown:

      • Modify DASH_COOLDOWN and WALL_HIT_COOLDOWN to adjust the cooldown times.
    • Event Functions:

      • Implement custom logic in onDashStart, onDashEnd, onDashInterrupted, and onPlayerHit functions as needed.

    Conclusion

    This setup provides a comprehensive dashing system that meets your requirements. It handles input detection, movement, collisions, and interaction with other players. Feel free to customize and extend the scripts to better fit your game's needs.