I’m trying to have input from a client be sent to the server in order for all clients to be able to see it. The issue is that when I try to receive the info from the server, it doesn’t seem to work with while loops to check if those inputs are being pressed.
I’ve tried putting if statements inside the while loop but it’s still not stopping when the condition is not met: https://gyazo.com/bad1ca62e8141a7d294c0c0a744a4120
Server script inside server script service:
local remoteEvents = game:GetService("ReplicatedStorage").remoteEvents
remoteEvents.moveDirection.OnServerEvent:Connect(function(player,up,down,left,right)
local dist = 1
remoteEvents.moveDirection:FireAllClients(player,up,down,left,right,dist)
end)
Local script inside “gameUI” in StarterGUI:
local uis = game:GetService("UserInputService")
local remoteEvents = game:GetService("ReplicatedStorage").remoteEvents
local upKey = false
local downKey = false
local leftKey = false
local rightKey = false
uis.InputBegan:Connect(function(i)
if i.KeyCode == Enum.KeyCode.W then
upKey = true
elseif i.KeyCode == Enum.KeyCode.S then
downKey = true
elseif i.KeyCode == Enum.KeyCode.A then
leftKey = true
elseif i.KeyCode == Enum.KeyCode.D then
rightKey = true
end
remoteEvents.moveDirection:FireServer(upKey,downKey,leftKey,rightKey)
end)
uis.InputEnded:Connect(function(i)
if i.KeyCode == Enum.KeyCode.W then
upKey = false
elseif i.KeyCode == Enum.KeyCode.S then
downKey = false
elseif i.KeyCode == Enum.KeyCode.A then
leftKey = false
elseif i.KeyCode == Enum.KeyCode.D then
rightKey = false
end
remoteEvents.moveDirection:FireServer(upKey,downKey,leftKey,rightKey)
end)
remoteEvents.moveDirection.OnClientEvent:Connect(function(player,up,down,left,right,dist)
while up do
wait()
print("Moving up...")
end
end)
If I replace the while loop with an if statement like as follows:
remoteEvents.moveDirection.OnClientEvent:Connect(function(player,up,down,left,right,dist)
if up then
print("Moving up...")
end
end)
then it seems to work fine with the text only printing when I begin pressing W.
https://gyazo.com/2d9c62fdbe397fee975c7f7e1ff56320
Maybe I’m doing this wrong? Maybe there’s an easier way that the information can be replicated to all clients? Really any help would be greatly appreciated!