As the title says, I’m trying to make a tank game. I’ve got the movement made with player inputs already, it’s just that, when I test it with 2 players, when the first player joins, movement is fine, when the second player joins, they can control their tank perfectly fine, but when the first player tries to control their tank, they instead control the second player’s tank. I’ve been trying to fix this for a while now, but I just can’t think of a way to do it, so I’m resorting to the dev forum yet again. Help
Here’s my code:
Server side:
local runService = game:GetService("RunService")
local fastCast = require(game.ReplicatedStorage.Modules.FastCastRedux)
local turningLeft
local turningRight
local movingForward
local movingBackward
local headTurningLeft
local headTurningRight
local turningSpeed = 0.02
local speed = 0.05
local turretTurningSpeed = 0.02
-- Movement
local events = game.ReplicatedStorage.Events.Movement
events.turnRight.OnServerEvent:Connect(function()
turningRight = true
end)
events.turnLeft.OnServerEvent:Connect(function()
turningLeft = true
end)
events.moveForward.OnServerEvent:Connect(function()
movingForward = true
end)
events.moveBackward.OnServerEvent:Connect(function()
movingBackward = true
end)
events.headTurnRight.OnServerEvent:Connect(function()
headTurningRight = true
end)
events.headTurnLeft.OnServerEvent:Connect(function()
headTurningLeft = true
end)
events.stopTurningRight.OnServerEvent:Connect(function()
turningRight = false
end)
events.stopTurningLeft.OnServerEvent:Connect(function()
turningLeft = false
end)
events.stopMovingForward.OnServerEvent:Connect(function()
movingForward = false
end)
events.stopMovingBackward.OnServerEvent:Connect(function()
movingBackward = false
end)
events.stopHeadTurningRight.OnServerEvent:Connect(function()
headTurningRight = false
end)
events.stopHeadTurningLeft.OnServerEvent:Connect(function()
headTurningLeft = false
end)
runService.Heartbeat:Connect(function(dt)
if turningRight == true then
playerTank.CFrame = playerTank.CFrame * CFrame.fromEulerAnglesXYZ(0,-turningSpeed,0 * dt)
end
if turningLeft == true then
playerTank.CFrame = playerTank.CFrame * CFrame.fromEulerAnglesXYZ(0,turningSpeed,0 * dt)
end
if movingForward == true then
playerTank.CFrame = playerTank.CFrame * CFrame.new(0,0,-1 * speed)
end
if movingBackward == true then
playerTank.CFrame = playerTank.CFrame * CFrame.new(0,0,1 * speed)
end
if headTurningRight == true then
local weld = playerTank.turretWeld
weld.C0 *= CFrame.Angles(0,-turretTurningSpeed, 0)
end
if headTurningLeft == true then
local weld = playerTank.turretWeld
weld.C0 *= CFrame.Angles(0,turretTurningSpeed, 0)
end
end)
Client side:
local userInputService = game:GetService("UserInputService")
local tankController = require(game.ReplicatedStorage.Modules.tankController)
userInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.A then
game.ReplicatedStorage.Events.Movement.turnLeft:FireServer()
end
if input.KeyCode == Enum.KeyCode.D then
game.ReplicatedStorage.Events.Movement.turnRight:FireServer()
end
if input.KeyCode == Enum.KeyCode.W then
game.ReplicatedStorage.Events.Movement.moveForward:FireServer()
end
if input.KeyCode == Enum.KeyCode.S then
game.ReplicatedStorage.Events.Movement.moveBackward:FireServer()
end
if input.KeyCode == Enum.KeyCode.G then
game.ReplicatedStorage.Events.Movement.headTurnRight:FireServer()
end
if input.KeyCode == Enum.KeyCode.F then
game.ReplicatedStorage.Events.Movement.headTurnLeft:FireServer()
end
end
end)
userInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.A then
game.ReplicatedStorage.Events.Movement.stopTurningLeft:FireServer()
end
if input.KeyCode == Enum.KeyCode.D then
game.ReplicatedStorage.Events.Movement.stopTurningRight:FireServer()
end
if input.KeyCode == Enum.KeyCode.W then
game.ReplicatedStorage.Events.Movement.stopMovingForward:FireServer()
end
if input.KeyCode == Enum.KeyCode.S then
game.ReplicatedStorage.Events.Movement.stopMovingBackward:FireServer()
end
if input.KeyCode == Enum.KeyCode.G then
game.ReplicatedStorage.Events.Movement.stopHeadTurningRight:FireServer()
end
if input.KeyCode == Enum.KeyCode.F then
game.ReplicatedStorage.Events.Movement.stopHeadTurningLeft:FireServer()
end
end
end)
I get “playerTank” (Server script) by firing an event on the client, passing the player’s name, and the server script picks it up:
game.ReplicatedStorage.Events.tankDeployed.OnServerEvent:Connect(function(player)
playerTank = game.ReplicatedStorage.Tanks.testTank:Clone()
playerTank.Name = tostring(player.Name.."Tank")
playerTank.Parent = game.Workspace
game.ReplicatedStorage.Events.tankDeployed:FireClient(player,playerTank)
end)