Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Made a steps leaderboard, says input is a nil value, any help?

I tried making a game that gives you points for walking. It does not work, because 'input' is a nil value in the server script. Here's the script:

LocalScript (in StarterGui):

local uis = game:GetService("UserInputService")
uis.InputBegan:connect(function(input)
	game.ReplicatedStorage.addValueToSteps:FireServer(input)
end)

Server Script (in ServerScriptService):

game.ReplicatedStorage.addValueToSteps.OnServerEvent:Connect(function(client, input)
	if input.KeyCode == Enum.KeyCode.A or Enum.KeyCode.S or Enum.KeyCode.W or Enum.KeyCode.D then 
		local steps = client.leaderstats.Steps 
		local char = client:FindFirstChild("Character") 
		if char then
			local walkSpeed = char.Humanoid.WalkSpeed
			steps.Value = steps.Value + walkSpeed / 10
		end
	end
end)

The problem is that it is stating that, on line 2, the 'input' parameter in the server script equals a nil value. What is happening with this?

2 answers

✓ Accepted answer

You shouldn't detect player movement like that there is a better way using a built in function that roblox has provided for this type of detection.

LocalScript

local Player = game.Players.LocalPlayer
local Character = workspace:WaitForChild(Player.Name)

Character:WaitForChild('Humanoid').Running:Connect(function(speed)
if speed > 0 then -- Player Is Walking
game.ReplicatedStorage.addValueToSteps:FireServer() 
else return
end
end)

ServerScript

game.ReplicatedStorage.addValueToSteps.OnServerEvent:Connect(function(player)
    local steps = player:WaitForChild('leaderstats').Steps 
    local character = workspace:WaitForChild(player.Name)
    if character then
        local walkSpeed = character:WaitForChild('Humanoid').WalkSpeed
        steps.Value = steps.Value + walkSpeed / 10
    end
end)

2 comments

0

is it possible to increase a player's steps by 1 for every half second that theyre walking? (thats the purpose of me making these scripts)

0

just divide it by two so in one full step they get the full value

Hi!

The way you are making this is not correct. The "input" value will return nil because the input have to be defined inside the InputBegan function. Here is how your script have to be like:

LocalScript

local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.A or Enum.KeyCode.S or Enum.KeyCode.W or Enum.KeyCode.D then
    	game.ReplicatedStorage.addValueToSteps:FireServer(input)
	end
end)

ServerScript

game.ReplicatedStorage.addValueToSteps.OnServerEvent:Connect(function(client)
	local steps = client.leaderstats.Steps 
	local char = client:FindFirstChild("Character") 
	if char then
		local walkSpeed = char.Humanoid.WalkSpeed
		steps.Value = steps.Value + walkSpeed / 10
	end
end)

I hope this works for you!

Sincerely, Ziruken

This discussion is preserved as it was. New answers, comments, and votes are closed.