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

Why does my dash Always happen... When it should only happen if "W" or the up arrow is pressed?

Asked by 5 years ago

Good Morinin'.. Kind of.. Anyway, hope your haivng a good day, could someone explain why my script is always calling the RemoteFunction on line 9? It only should happen if it was pressed twice in 1 second... Thanks in advance :D

Input.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.Up then
        local timeing = tick()
        Input.InputBegan:Connect(function(key)
            if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.Up then
                local timez = tick() 
                local timing = (timeing - timez)
                if timing <= 1 then
                    game.Workspace.DashScript.DashDesa:FireServer()


                elseif timing > 1 then 



            end
            end
        end)

    end
end)
0
thats a remote event lol User#23365 30 — 5y
0
Nani? (I'm confused...) ScriptingNubs 55 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You have the right idea for the most part!

The reason using two InputBegan:Connect()'s isn't working is because it's just checking 2 times if W is pressed. Not checking if W is pressed, 2 times. Anyway

To achieve this I would use:
- Local Script in StarterPlayerScripts
- Remote Event in ReplicatedStorage named "DashEvent"
- Script in ServerScriptService named "DashScript"

First starting with the Local Script

--Get Services
local Input = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
--Get your Remote Event
local dashEvent = RS:FindFirstChild("DashEvent")

--Variables
local lastKey = false 
local lastClick --For setting the lastClick's tick()
local DASH_TIME = .5 --Time you want the double click to be within (you had 1)

Input.InputBegan:connect(function(KeyInput)
    --Check the keycode
    if KeyInput.KeyCode == Enum.KeyCode.W then
    --Check if a last key is pressed, if lastKey not pressed, then set it to pressed now.
        if not lastKey then
            lastKey = true
        end
        --there are 2 presses of the key and they are within the "DASH TIME" 
        if lastClick ~= nil and tick() - lastClick <= DASH_TIME then 
            print("Dash")
            --Fire your Remote Event
            dashEvent:FireServer(true) --This already sends the player as a parameter and we are adding a booleon marking if we are running or not.
            --Then set last key to not pressed (to reset)
            lastKey = false
        else
            lastClick = tick()
        end
    end
end)

--Reset speed when you stop sprinting
Input.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.W then
        print("LIFTED")
        dashEvent:FireServer(false) ---This already sends the player as a parameter and we are adding a booleon marking if we are running or not.
        end
end)

Now with your local script set up, I've placed a Script in ServerScriptService called "DashScript"

Pretty Straight forward, at the bottom we listen for the Event to Fire and run the Speed Adjust function when it does. Like we saw in the local script, SpeedAdjust is getting 2 arguments, the player and the runningBool.

local RS = game:GetService("ReplicatedStorage")
local dashEvent = RS:FindFirstChild("DashEvent")

local SPEED_INC = 100 --Speed increase, can be whatever, 100 is just fun
local NORMAL  = 16 --defualt

local function SpeedAdjust (player, runningBool) --runningBool is the true/false you are sending over from the RemoteEvent
    local hum = player.Character:WaitForChild("Humanoid")
    if hum and runningBool then
        print("SPRINT")
        hum.WalkSpeed = hum.WalkSpeed + SPEED_INC
    elseif hum and not runningBool then
        print("SLOW")
        hum.WalkSpeed = NORMAL
    end
end


dashEvent.OnServerEvent:Connect(SpeedAdjust)

There might be a better way to do the stop sprinting stuff but this works just fine. Let me know if you have any questions and don't forget to accept the answer if it helps you out :)

Ad

Answer this question