How can i do this in a script?

How do you make a script whereas when the players humanoid function is walking a value increases? since i tried doing this with the input began function and it works but its not mobile supported, and i want to be supported for both devices, so how can i do this?
this is the code

local uis = game:GetService("UserInputService")
local walking = false
local stepswalked = 0
local player = game.Players.LocalPlayer
local points = player:WaitForChild("Points")
local multiplier = player:WaitForChild("PointsMultiplier").Value



uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then 
		if input.KeyCode == Enum.KeyCode.W then 
			walking = true
			
			while walking == true do 
				points.Value = (points.Value + 1) * multiplier
				wait()
			end
			elseif input.KeyCode == Enum.KeyCode.S then 
				walking = true

				while walking == true do 
				points.Value = (points.Value + 1) * multiplier
				wait()
				end
			
		elseif input.KeyCode == Enum.KeyCode.D then 
			walking = true

			while walking == true do 
				points.Value = (points.Value + 1) * multiplier
				wait()
			end
	elseif input.KeyCode == Enum.KeyCode.A then 
		walking = true

		while walking == true do 
				points.Value = (points.Value + 1) * multiplier
			wait()
		end
		

	end
	end
end)

uis.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then 
		if input.KeyCode == Enum.KeyCode.W then 
			walking = false
		elseif input.KeyCode == Enum.KeyCode.S then 
			walking = false
		elseif input.KeyCode == Enum.KeyCode.D then 
			walking = false

	elseif input.KeyCode == Enum.KeyCode.A then 
		walking = false

	end
	end
end)


You can use the state of the humanoid. It would look like:
While Humanoid.Walking = true do Value = Value + 1 -- change to the amount you want to give per second Wait(1)

1 Like

this script will work but i think there are better ways to do it

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid :Humanoid = Character:WaitForChild("Humanoid")

local value = 0 -- the value you want to increase


RunService.Heartbeat:Connect(function()
	if Humanoid.MoveDirection.Magnitude > 0 then -- checks for the player` moving direction
		value += 1 
		print(value)
	end
end)

it checks if the player is moving and if he is moving it will increment the value it will work on any device whenever the character is moved
:warning: it is based of the player fps so a player with 120 fps will get 2x the stats from a player with 60fps if you donot want that you can use delta or add it in a while true do loop

if you want to use deltaTime you can replace

value += 1

with

value += delta * 60
1 Like

whats a delta in a script? sorry if i sound dumb

its the time passed since the last frame
you can check it on yt for better explanation

ok, ill check it on youtube then

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.