Walking to Increase a Value?

I am trying to make a value increase as you walk. I set up a code that checks if the player is pressing W, A, S, or D and will fire an event that increases the value, however I am struggling to find a way that will repeat for the entire duration that the keys are held down. Here is the script;

Script
local Player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local Debounce = true


UIS.InputBegan:connect(function(key, Plr)
	if Debounce == true then
		if key.KeyCode == Enum.KeyCode.W or Enum.KeyCode.D or Enum.KeyCode.A or Enum.KeyCode.S then
			Debounce = false
			game.ReplicatedStorage.Speed:FireServer()
			wait(0.1)
			Debounce = true
		end
	end
end)

(I fire an event so that the value increases on server for data store)

2 Likes

use UIS.InputEnded, this will detect if the input ended or not.
Edit: i swear that there is a step service or something, I saw it on YT

If you’re fine with just Player movement, you could use the HumanoidRootPart’s Velocity.

local val = 0
while wait() do
if HumanoidRootPart.Velocity.Magnitude > 1 then
val = val + 1
end
end

https://developer.roblox.com/en-us/api-reference/property/BasePart/Velocity

2 Likes

what if there was emoting? An animation can change the root part

1 Like

I’m pretty sure the HumanoidRootPart stays still with animations.
Every other bodypart is relative to the HumanoidRootPart but the HumanoidRootPart is not animated.

3 Likes

If I am getting humanoid root part would the script have to be in starter character script?

yes or you would have to wait for the HumanoidRootPart

Player = game.Players.LocalPlayer
Char = Player.Character
HumanoidRootPart = Char:WaitForChild('HumanoidRootPart')

With both of these solutions the remote event is only fired once…

I need the remote event to fire ever 0.3ish seconds while the player is moving.

You should handle the checks & object manipulation on the server, not the client.

1 Like

Try this. I used the Heartbeat function of the RunService instead of the while loop so other script can run simultaneously in the same script. I’ve also used @dispeller 's method of using the Magnitude of the humanoid. Hope this helps!

local Player = game.Players.LocalPlayer
local HumanoidRootPart = Player.Character:WaitForChild("HumanoidRootPart")
local RS = game:GetService("RunService")
local debounce = false

local function testDebounce()
	if debounce then
		return
	else
		debounce = true
		game.ReplicatedStorage.Speed:FireServer()
		wait(0.3) --How ever long you want your delay between event calls
		debounce = false
	end
end

RS.Heartbeat:Connect(function()
	if HumanoidRootPart.Velocity.Magnitude > 1 then
		testDebounce()
	end
end)
1 Like

Can I ask why exactly you’re doing this, what is the data store for.

It stores the players’ speed. All it does is fire a remote event that triggers this; Speed.Value = Speed.Value + 1. If I don’t do this then Speed is stored on the client and I can’t save it with data store

I’m just concerned that maybe you’re asking how to solve problem for a solution rather than the underlying problem itself, what is the data store there for, are you just having a mess about with it is what I mean

I’m not really sure what you’re asking. The datastore is completely different and won’t affect the problem I am having with the remote event only firing once.

Don’t worry about it, for some reason you want a data store for the player’s speed, with no reason why. I was just thinking maybe there’s a better way of doing whatever you’re doing with the data store.

No, no, no. The datastore is seperate. It fires an event to add to the players’ IntValue. The script is a local script in StatterCharacterScripts and if I didn’t fire a remote event to increase the speed then the datastore wouldn’t work because the IntValue would have only been updated on the client.

Not sure if you edited it or I am just tired, but the game is called Flash Simulator where a value Speed increases as you walk.

Hello, just curious about two things. Did AstonAceMan’s solution work for you? Or did it yield the same results, I didn’t see a reply from you explicitly stating your results.

Also from your statement you mention:

All it does is fire a remote event that triggers this;
Speed.Value = Speed.Value + 1

Is that all that’s happening when it’s triggered or are you also updating your DataStore in the same bit?

I know this isn’t exactly relevant to your problem at hand, but do you verify this at all on the server side, so say John Doe can’t come along and fire the remote as much as he likes?

You can just use the @AstonAceMan’s method but handled by the server so you don’t have to worry about an exploiter firing a Remote Event to get points:

local Players = game:GetService("Players")
local RS = game:GetService("RunService")
local debounce = false

Players.PlayerAdded:Connect(function(player)
	local SpeedValue =  --Rute of the Speed value
	
	RS.Heartbeat:Connect(function()
        local Character = player.Character or player.CharacterAdded:Wait()
	    local HumanoidRootPart = Character.HumanoidRootPart
		if HumanoidRootPart.Velocity.Magnitude > 1 then
			if debounce then
				return
			else
				debounce = true
				SpeedValue.Value = SpeedValue.Value + 1
				wait(0.3)
				debounce = false
			end
		end
	end)
end)
1 Like

I tested my code but with a print function and it worked. Did you get the same results with the event?
If it’s what you want then great, but I would definitely recommend @geovanny4567 ‘s method on the server.

1 Like

Yep it worked, thanks!

30chars