Hi there, I have a local script that fires a remote event when a key is pressed and then on the serverscript receives the event and prints out a line. I am wondering how can I loop a print continuously for when a key is held, and then stops when the key is released. How can I achieve this? I’ve tried using iskeydown, but still for some reason didn’t work.
ls
local forward = game.ReplicatedStorage:WaitForChild("forward")
local reverse = game.ReplicatedStorage:WaitForChild("reverse")
local left = game.ReplicatedStorage:WaitForChild("left")
local right = game.ReplicatedStorage:WaitForChild("right")
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.T then
forward:FireServer(player)
end
if input.KeyCode == Enum.KeyCode.G then
reverse:FireServer(player)
end
if input.KeyCode == Enum.KeyCode.F then
left:FireServer(player)
end
if input.KeyCode == Enum.KeyCode.H then
right:FireServer(player)
end
end)
ss
forward.OnServerEvent:Connect(function(player)
if player.Name == "vxsqi" then
print("t")
end
end)
reverse.OnServerEvent:Connect(function(player)
if player.Name == "vxsqi" then
print("g")
end
end)
left.OnServerEvent:Connect(function(player)
if player.Name == "vxsqi" then
print("f")
end
end)
right.OnServerEvent:Connect(function(player)
if player.Name == "vxsqi" then
print("h")
end
end)
put a UIS.InputEnded as well and then have a variable to check if the key is being held or not (set it to true on inputbegan, set it to false on inputended). Fire the server every time the value is changed and the rest is pretty easy.
Can you elaborate more on this, I don’t seem to understand what you mean. Do you mean adding a variable that is set to false, and when the key is held and release it sets it to true? Are you talking about a while loop aswell, because I tried this and still can’t get it to work. I don’t have the script for this attempt.
local gen= game.ReplicatedStorage:WaitForChild("general")
local texts = {"forward","reverse","left","right"}
local inputsPressed = {
false,
false,
false,
false
}
local keyInputs = {
Enum.KeyCode.T,
Enum.KeyCode.G,
Enum.KeyCode.F,
Enum.KeyCode.H
}
UIS.InputBegan:Connect(function(input)
for i,key in pairs(keyInputs) do
if (input.KeyCode == key) then
inputsPressed[i] = true;
end
end
end)
UIS.InputEnded:Connect(function(input)
for i,key in pairs(keyInputs) do
if (input.KeyCode == key) then
inputsPressed[i] = false;
end
end
end)
while wait() do
for i,bool in pairs(inputsPressed) do
if (bool == true) then
gen:FireServer(texts[i])
end
end
end
general.OnServerEvent:Connect(function(player,text)
if player.Name == "vxsqi" then
print(tostring(text))
end
end)
Ok, note first that you don’t need the player argument when firing server
Anyways, what you will pass in the FireServer is a boolean. On the InputBegan pass a true, on the InputEnded pass a false.
On the server, add variables to check if a key is pressed. Then the server will recieve the player who fired, and the boolean passed. Set the variables to the boolean. Then make a while loop checking the variables pressed.
UIS.InputBegan:Connect(function()
if input.KeyCode == Enum.KeyCode.T then
forward:FireServer(true)
end
if input.KeyCode == Enum.KeyCode.G then
reverse:FireServer(true)
end
if input.KeyCode == Enum.KeyCode.F then
left:FireServer(true)
end
if input.KeyCode == Enum.KeyCode.H then
right:FireServer(true)
end
end)
UIS.InputEnded:Connect(function()
if input.KeyCode == Enum.KeyCode.T then
forward:FireServer(false)
end
if input.KeyCode == Enum.KeyCode.G then
reverse:FireServer(false)
end
if input.KeyCode == Enum.KeyCode.F then
left:FireServer(false)
end
if input.KeyCode == Enum.KeyCode.H then
right:FireServer(false)
end
end)
On the server
local isForward = false
local isReverse = false
local isLeft = false
local isRight = false
forward.OnServerEvent:Connect(function(player)
if player.Name == "vxsqi" then
print("t")
isForward = pressed
end
end)
reverse.OnServerEvent:Connect(function(player)
if player.Name == "vxsqi" then
print("g")
isReverse = pressed
end
end)
left.OnServerEvent:Connect(function(player)
if player.Name == "vxsqi" then
print("f")
isLeft = pressed
end
end)
right.OnServerEvent:Connect(function(player,pressed)
if player.Name == "vxsqi" then
print("h")
isRight = pressed
end
end)
while true do
task.wait(0.1)
if isForward then
-- Is forward
end
if isReverse then
-- Is backwards
end
if isLeft then
-- Is left
end
if isRight then
-- Is right
end
end
quick example, untested, and probably doesn’t work
local:
local isHolding = false
local rEvent = --blah
UIS.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.KeyCode == Enum.KeyCode.T then
isHolding = true
rEvent:FireServer(isHolding)
end
end
end)
UIS.InputEnded:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.KeyCode == Enum.KeyCode.T then
isHolding = false
rEvent:FireServer(isHolding)
end
end
end)
server:
local isHoldingSS = false
rEvent.OnServerEvent:Connect(function(isHolding)
isHoldingSS = isHolding
end)
while true do
wait(0.1) --optional
if isHoldingSS == true then
print("text")
end
end
well if it wasn’t there then nothing would happen since the server would think that it’s always false and the loop would never activate. (i think, I’m pretty tired right now so i might be being dumb)