How to make a turret that can be controlled with WASD?

That might be because its trying to rotate in both directions?
To prioritize the most recent input, you should Pause the other tween before Playing one
like replace

RotateTween:Play()

with

Rotate:Pause() --incase you renamed it, then replace Rotate here with that
RotateTween:Play()

& same with the other

. . .
Rotate:Play()

to

RotateTween:Pause()
Rotate:Play()

image

like this:

local StartActions = {
	ActionW = function(player) --Function when W is pressed
		Rotate:Pause()
		RotateTween:Play()
	end;
	ActionA = function(player) --Function when A is pressed
		print(player.Name.." pressed A")
	end;
	ActionS = function(player) --Function when S is pressed
		RotateTween:Pause()
		Rotate:Play()
	end;
	ActionD = function(player) --Function when D is pressed
		print(player.Name.." pressed D")
	end;
}

local EndActions = {
	ActionW = function(player) --Function when W is no longer pressed
		RotateTween:Pause()
	end;
	ActionA = function(player) --Function when A is no longer pressed
		print(player.Name.." stopped pressing A")
	end;
	ActionS = function(player) --Function when S is no longer pressed
		Rotate:Pause()
	end;
	ActionD = function(player) --Function when D is no longer pressed
		print(player.Name.." stopped pressing D")
	end;
}
1 Like

the same problem keeps happening

This should work!

Paste this between the local Rotate line and the local StartActions line;

RotateTween.Completed:Connect(function(state)
	if state == Enum.PlaybackState.Completed then
		RotateTween:Play()
	end
end)
Rotate.Completed:Connect(function(state)
	if state == Enum.PlaybackState.Completed then
		Rotate:Play()
	end
end)

and replace TI with this:

local TI = TweenInfo.new(10,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0)

Is there a way to lock the rotation?
For example, the part only rotates along the Y axis
When you rotate the part for a long time it starts to rotate randomly.

Yeah I’ve noticed that it goes crazy after a few rotations and I’ve tried everything to fix it, but didn’t really find any solutions :​(

Hopefully someone else can help in solving that

1 Like

its ok you helped me a lot
can you organize the script so i can pin it as the Solution?
:3

Are you sure?
Others will see it marked as Solved afterwards and probably wont view the post

i think i will make a new topic
and i will keep this one open

Wait, I might’ve found a solution!
Normal/Server Script:

local TI = TweenInfo.new(10,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,-1,false,0)
local RotateTween
local Rotate

local StartActions = {
	ActionW = function(player)
		print("+W")
		if Rotate then Rotate:Pause() end
		RotateTween = game:GetService("TweenService"):Create(workspace.Part,TI,{Rotation = workspace.Part.Rotation+Vector3.new(0,360,0)})
		RotateTween:Play()
	end;
	ActionA = function(player)
		print("+A")
	end;
	ActionS = function(player)
		print("+S")
		if RotateTween then RotateTween:Pause() end
		Rotate = game:GetService("TweenService"):Create(workspace.Part,TI,{Rotation = workspace.Part.Rotation+Vector3.new(0,-360,0)})
		Rotate:Play()
	end;
	ActionD = function(player)
		print("+D")
	end;
}

local EndActions = {
	ActionW = function(player)
		print("-W")
		RotateTween:Pause()
	end;
	ActionA = function(player)
		print("-A")
	end;
	ActionS = function(player)
		print("-S")
		Rotate:Pause()
	end;
	ActionD = function(player)
		print("-D")
	end;
}

game.ReplicatedStorage.TurretRemote.OnServerEvent:Connect(function(player,began,key)
	if began then
		StartActions["Action"..key](player)
	else
		EndActions["Action"..key](player)
	end
end)
1 Like

it works but it create another problem :sweat_smile:
when you press for example W for too long and you switch to S it keep spinning at the same direction
the output
W+
W-
S+
S-

Yeah I give up lmao :sob::pray:

I actually have no idea why its doing that

1 Like

i think we need to sacrifice someone’s life to satan to fix this bug :shushing_face:

1 Like

All these bugs happen and we’re just trying to create a simple turret. :sob:

1 Like

I’m sorry, the current way you’re doing it seems way too complicated for no reason.
Here’s my take:
client:

local players = game:GetService "Players"
local replicated_storage = game:GetService "ReplicatedStorage"
local context_action_service = game:GetService "ContextActionService"

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:FindFirstChildOfClass "Humanoid"

local remote = replicated_storage:WaitForChild "TurretInput"

humanoid.Seated:Connect(function(seated: boolean, seat: Seat)
--if not seat:HasTag "TurretSeat" then return end
	if seated then
		context_action_service:BindActionAtPriority("TurretInput", function(_, state: Enum.UserInputState, input: InputObject)
			if state ~= Enum.UserInputState.Begin then return end
			remote:FireServer(input.KeyCode)
		end, false,10001, Enum.PlayerActions.CharacterForward, Enum.PlayerActions.CharacterBackward, Enum.PlayerActions.CharacterLeft, Enum.PlayerActions.CharacterRight)
	else
		context_action_service:UnbindAction "TurretInput"
	end
end)

server:

local replicated_storage = game:GetService "ReplicatedStorage"
local tween_service = game:GetService "TweenService"

local remote = replicated_storage.TurretInput
local turret_part = workspace.Turret.Head

remote.OnServerEvent:Connect(function(player: Player, input: Enum.KeyCode)
	if typeof(input) ~= "EnumItem" then return end
	if input ~= Enum.KeyCode.W and input ~= Enum.KeyCode.S then return end
	
	tween_service
		:Create(turret_part, TweenInfo.new(0.5), {Orientation = turret_part.Orientation + Vector3.new(0, input == Enum.KeyCode.W and 25 or -25, 0)})
	:Play()	
end)

first of all, you only want to use the wasd to rotate the turret whenever the player is seated, so use the seated event and ContextActionService (which outclasses UserInputService most of the time) to bind/unbind the input function whenever the player is seated or unseated. Then you send the keycode to the server and the server will rotate the turret by 25 depending on if it was W or S.

An alternate way is to setnetworkownership of the turret to the player, and then rotating the turret on the client.

hope this helps.
Here’s showcase video:


W for right, S for left, and I was spamming W to see if I could knock myself off because I forgot to anchor the seat.

1 Like

sorry for late response
is this script good?
i’m trying to keep it simple
this is the new script:

local RunService = game:GetService("RunService")

local ROTATION_SPEED = 60 -- degrees per second
local rotatingLeft = false
local rotatingRight = false

RunService.Heartbeat:Connect(function(dt)
	local part = workspace.Part
	if rotatingLeft then
		part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(ROTATION_SPEED * dt), 0)
	elseif rotatingRight then
		part.CFrame = part.CFrame * CFrame.Angles(0, -math.rad(ROTATION_SPEED * dt), 0)
	end
end)

game.ReplicatedStorage.TurretRemote.OnServerEvent:Connect(function(player, began, key)
	if key == "W" then
		rotatingLeft = began
	elseif key == "S" then
		rotatingRight = began
	end
end)

1 Like

Just to add onto this, move the turret on the client too for absolute smoothness

can you explain more i don’t understand what do you mean?

Move the turret on the client aswell

1 Like