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 6 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

01Input.InputBegan:Connect(function(key)
02    if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.Up then
03        local timeing = tick()
04        Input.InputBegan:Connect(function(key)
05            if key.KeyCode == Enum.KeyCode.W or key.KeyCode == Enum.KeyCode.Up then
06                local timez = tick()
07                local timing = (timeing - timez)
08                if timing <= 1 then
09                    game.Workspace.DashScript.DashDesa:FireServer()
10 
11 
12                elseif timing > 1 then
13 
14 
15 
View all 21 lines...
0
thats a remote event lol User#23365 30 — 6y
0
Nani? (I'm confused...) ScriptingNubs 55 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 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

01--Get Services
02local Input = game:GetService("UserInputService")
03local RS = game:GetService("ReplicatedStorage")
04--Get your Remote Event
05local dashEvent = RS:FindFirstChild("DashEvent")
06 
07--Variables
08local lastKey = false
09local lastClick --For setting the lastClick's tick()
10local DASH_TIME = .5 --Time you want the double click to be within (you had 1)
11 
12Input.InputBegan:connect(function(KeyInput)
13    --Check the keycode
14    if KeyInput.KeyCode == Enum.KeyCode.W then
15    --Check if a last key is pressed, if lastKey not pressed, then set it to pressed now.
View all 38 lines...

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.

01local RS = game:GetService("ReplicatedStorage")
02local dashEvent = RS:FindFirstChild("DashEvent")
03 
04local SPEED_INC = 100 --Speed increase, can be whatever, 100 is just fun
05local NORMAL  = 16 --defualt
06 
07local function SpeedAdjust (player, runningBool) --runningBool is the true/false you are sending over from the RemoteEvent
08    local hum = player.Character:WaitForChild("Humanoid")
09    if hum and runningBool then
10        print("SPRINT")
11        hum.WalkSpeed = hum.WalkSpeed + SPEED_INC
12    elseif hum and not runningBool then
13        print("SLOW")
14        hum.WalkSpeed = NORMAL
15    end
16end
17 
18 
19dashEvent.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