Tank Turret Movement Script is not Working (No errors/cues)

  1. What do you want to achieve? An answer, solution, result, anything to work or find error to the movement of my tank.

  2. What is the issue? For some reason–despite the working function of the “UpArrow” and “DownArrow” parts of the code (respectively to the keyboard)–the “LeftArrow” and “RightArrow” parts of the code don’t work (again, respective to the keyboard) with no errors showing up the output relating to the global script or local script using UserInputService (located in StarterPlayerScripts) in action. Strangely, other events in the same local script, including the up and down arrow parts of the global script, function.

fixture2

fixture3

fixture4

I have taken notice that the Hinge Constraint’s rotation works when changing it manually, where as an example: if I left the AngularVelocity as 0.3 before running an instance on Roblox Studio, it functions properly both in client and server. Another example of this is changing it on an active instance, where that functions with no issues, client or server.

AngularVelocity Not Changed

AngularVelocity Changed

I have also taken notice that the up and down arrow parts do visually change (as in being seen in the properties) the AngularVelocity but the the left and right arrow parts do not visually change the AngularVelocity (as in being seen in the properties).

  1. What solutions have you tried so far? I have looked online with multiple variations of searches, but I do not think any of them have hit the correct question I need to be asking. I have looked throughout the Devforum but I cannot the answers needed. I have looked on YouTube and haven’t found the proper solution. I even tried some testing with printing and copy and pasted working code (of course with modifications to make it applicable). Maybe I’m not looking as in-depth, but I have nothing to start off with.

(Full scripts below)

local ArrowUpEvent = game:GetService("ReplicatedStorage"):WaitForChild("ArrowUpEvent")
local ArrowDownEvent = game:GetService("ReplicatedStorage"):WaitForChild("ArrowDownEvent")
local ArrowLeftEvent = game:GetService("ReplicatedStorage"):WaitForChild("ArrowLeftEvent")
local ArrowRightEvent = game:GetService("ReplicatedStorage"):WaitForChild("ArrowRightEvent")

ArrowDownEvent.OnServerEvent:Connect(function(plr, truf)

	if truf == true then
		script.Parent.Turret.HingeConstraint.AngularVelocity = -0.3
		script.Parent.TurretBase.TurretStart:Play()
		script.Parent.TurretBase.TurretLoop:Play()
	elseif truf == false then
		script.Parent.Turret.HingeConstraint.AngularVelocity = 0
		script.Parent.TurretBase.TurretLoop:Stop()
		script.Parent.TurretBase.TurretStop:Play()
	end
end)

ArrowUpEvent.OnServerEvent:Connect(function(plr, truf)

	if truf == true then
		script.Parent.Turret.HingeConstraint.AngularVelocity = 0.3
		script.Parent.TurretBase.TurretStart:Play()
		script.Parent.TurretBase.TurretLoop:Play()
	elseif truf == false then
		script.Parent.Turret.HingeConstraint.AngularVelocity = 0
		script.Parent.TurretBase.TurretLoop:Stop()
		script.Parent.TurretBase.TurretStop:Play()
	end
end)

ArrowLeftEvent.OnServerEvent:Connect(function(plr, truf)

	if truf == true then
		script.Parent.Hull.HingeConstraint.AngularVelocity = 0.3
		script.Parent.TurretBase.TurretStart:Play()
		script.Parent.TurretBase.TurretLoop:Play()
	elseif truf == false then
		script.Parent.Hull.HingeConstraint.AngularVelocity = 0
		script.Parent.TurretBase.TurretLoop:Stop()
		script.Parent.TurretBase.TurretStop:Play()
	end
end)

ArrowRightEvent.OnServerEvent:Connect(function(plr, truf)

	if truf == true then
		script.Parent.Hull.HingeConstraint.AngularVelocity = -0.3
		script.Parent.TurretBase.TurretStart:Play()
		script.Parent.TurretBase.TurretLoop:Play()
	elseif truf == false then
		script.Parent.Hull.HingeConstraint.AngularVelocity = 0
		script.Parent.TurretBase.TurretLoop:Stop()
		script.Parent.TurretBase.TurretStop:Play()
	end
end)

uis.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Up and not processed then
		game:GetService("ReplicatedStorage").ArrowUpEvent:FireServer(true)
	end
end)

uis.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Down and not processed then
		game:GetService("ReplicatedStorage").ArrowDownEvent:FireServer(true)
	end
end)

uis.InputEnded:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Up and not processed then
		game:GetService("ReplicatedStorage").ArrowUpEvent:FireServer(false)
	end
end)

uis.InputEnded:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Down and not processed then
		game:GetService("ReplicatedStorage").ArrowDownEvent:FireServer(false)
	end
end)

uis.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Left and not processed then
		game:GetService("ReplicatedStorage").ArrowLeftEvent:FireServer(true)
	end
end)

uis.InputBegan:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Right and not processed then
		game:GetService("ReplicatedStorage").ArrowRightEvent:FireServer(true)
	end
end)

uis.InputEnded:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Left and not processed then
		game:GetService("ReplicatedStorage").ArrowLeftEvent:FireServer(false)
	end
end)

uis.InputEnded:Connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Right and not processed then
		game:GetService("ReplicatedStorage").ArrowRightEvent:FireServer(false)
	end
end)
1 Like

not a fix … your script is doing too many Connects

local arrowEvents = {
	[Enum.KeyCode.Up] = game:GetService("ReplicatedStorage").ArrowUpEvent,
	[Enum.KeyCode.Down] = game:GetService("ReplicatedStorage").ArrowDownEvent,
	[Enum.KeyCode.Left] = game:GetService("ReplicatedStorage").ArrowLeftEvent,
	[Enum.KeyCode.Right] = game:GetService("ReplicatedStorage").ArrowRightEvent
}

uis.InputBegan:Connect(function(input, processed)
	if not processed and arrowEvents[input.KeyCode] then
		arrowEvents[input.KeyCode]:FireServer(true)
	end
end)

uis.InputEnded:Connect(function(input, processed)
	if not processed and arrowEvents[input.KeyCode] then
		arrowEvents[input.KeyCode]:FireServer(false)
	end
end)

Thank you for the optimization! This code is in its very early stages and I was trying to combat the issues with shortening it, this helps not only for arrow keys but other keys.

1 Like

That is really cut down but, even if you don’t do it that way you don’t need so many connects.
Two would cover it no matter what … the key tests go within them two.

Looks like no one tried to answered this… :worried:

Ensure script.Parent.Hull.HingeConstraint exists and is configured correctly for motorized rotation.
If that isn’t it … Maybe try replacing script.Parent.Hull.HingeConstraint with a direct reference

local hullConstraint = script.Parent:FindFirstChild("Hull") and script.Parent.Hull:FindFirstChild("HingeConstraint")

ArrowLeftEvent.OnServerEvent:Connect(function(plr, truf)
    if hullConstraint then
        hullConstraint.AngularVelocity = truf and 0.3 or 0
    end
end)

ArrowRightEvent.OnServerEvent:Connect(function(plr, truf)
    if hullConstraint then
        hullConstraint.AngularVelocity = truf and -0.3 or 0
    end
end)

I have my turret follow the mouse, so this is untested.

Coming back to this, I believe I found the solution but not a way to implement it. Basically, the camera has arrow controls–as you know–and these controls’ priorities are higher than the UserInputService detections for arrow movement. I tested what would happen if the keybinds were say 0 and 9, and it worked. Priorities are applied via the ContextActionService. So it seems like script.Parent.Hull.HingeConstraint is properly defined BUT it is being overrided.

1 Like