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

Script Won't Print Statement After Pressing Shift Key?

Asked by 8 years ago

So, I've been working on a script that activates increases your speed when you press Left Shift. I've recently been trying to just get print statements to work so that I know something being transferred over the RemoteEvent I set up. Well, the print statement doesn't work, and the output is left blank. So, here's the code inside StarterPlayerScripts:

local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local event = game.ReplicatedStorage:WaitForChild("SprintEvent")

if userInputService.InputBegan and userInputService.KeyboardEnabled then
    userInputService.InputBegan:connect(function(inputObject)
    if inputObject == Enum.KeyCode.LeftShift then       
            event:FireServer("SprintBegan")
        end 
    end)
elseif userInputService.InputEnded and userInputService.KeyboardEnabled then    
    userInputService.InputEnded:connect(function(inputObject)
        if inputObject == Enum.KeyCode.LeftShift then                       
            event:FireServer("SprintEnded")
        end 
    end)
end 

and here's the test script inside ServerScriptService:

local sprintEvent = game.ReplicatedStorage:WaitForChild("SprintEvent")

sprintEvent.OnServerEvent:connect(function(...)
    local tuple = {...}
    if tuple[1] == "SprintBegan" then
        print("Sprint Began")
    elseif tuple[1] == "SprintEnded" then
        print("Sprint Ended")
    end
end)

1 answer

Log in to vote
0
Answered by
XAXA 1569 Moderation Voter
8 years ago

OnServerEvent always takes the player as its first argument, THEN the other arguments.

local sprintEvent = game.ReplicatedStorage:WaitForChild("SprintEvent")

sprintEvent.OnServerEvent:connect(function(player, ...)
    local tuple = {...}
    if tuple[1] == "SprintBegan" then
        print("Sprint Began")
    elseif tuple[1] == "SprintEnded" then
        print("Sprint Ended")
    end
end)
Ad

Answer this question