Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
1
Answered by
Prestory 1395 Moderation Voter
4 years ago
Edited 4 years ago

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)
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) User#28017 0 — 4y
0
just divide it by two so in one full step they get the full value Prestory 1395 — 4y
Ad
Log in to vote
1
Answered by
Ziruken 139
4 years ago

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

Answer this question