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?
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)
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