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

im new to roblox studio and i want to make WASD controlled turret but i don’t know where to start

3 Likes

You can use UserInputService.InputBegan and check if the input was W, A, S or D (Enum.Keycode) then do the corresponding actions and use UserInputService.InputEnded to end them

If you could provide me with a basic script or a tutorial it would be very helpful because I don’t understand what you mean. :slightly_smiling_face:

1 Like

my bad, lol

localscript:

local UIS = game:GetService("UserInputService")

local Actions = {
	ActionW = function() --Function when W is pressed
		print("W was pressed")
	end;
	ActionA = function() --Function when A is pressed
		print("A was pressed")
	end;
	ActionS = function() --Function when S is pressed
		print("S was pressed")
	end;
	ActionD = function() --Function when D is pressed
		print("D was pressed")
	end;
}

UIS.InputBegan:Connect(function(input,gpe)
	local Action = Actions["Action"..input.KeyCode.Name]
	if Action then
		Action()
	end
end)

something like this

4 Likes

this only works in localscript?

can you explain more how it works
like what if i want to connected with a normal script in workspace?

yeah, if you want to control it on the server, id create a RemoteEvent, and run the functions on a normal script

For these scripts, put a remoteevent called “TurretRemote” in ReplicatedStorage

LocalScript:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
	if string.find("W A S D",input.KeyCode.Name) then
		game.ReplicatedStorage.RemoteEvent:FireServer(true,input.KeyCode.Name)
	end
end)

UIS.InputEnded:Connect(function(input)
	if string.find("W A S D",input.KeyCode.Name) then
		game.ReplicatedStorage.TurretRemote:FireServer(false,input.KeyCode.Name)
	end
end)

ServerScript (Normal Script):

local StartActions = {
	ActionW = function(player) --Function when W is pressed
		print(player.Name.." pressed W")
	end;
	ActionA = function(player) --Function when A is pressed
		print(player.Name.." pressed A")
	end;
	ActionS = function(player) --Function when S is pressed
		print(player.Name.." pressed S")
	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
		print(player.Name.." stopped pressing W")
	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
		print(player.Name.." stopped pressing S")
	end;
	ActionD = function(player) --Function when D is no longer pressed
		print(player.Name.." stopped pressing 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)

let’s say i want to control a part in workspace like if i press W and im sitting in seat(i need to detect the seatweld?) the part will rotate 360(cframe?)

to detect if a player is sitting, use the statement Humanoid.SeatPart ~= nil (if they arent sitting SeatPart is nil)

and what exactly do you mean by rotate 360?
like smoothly, and stop when W isnt pressed anymore?

it will rotate when the player are pressing W

Better yet- use Humanoid.Seated
Humanoid.Seated:Connect:(function()

That would fire the function when the player just seated, OP wants it to fire on the press of W A S D keys

to rotate the part, put this on top of the Server/Normal Script;
(if you want to, replace the first 1 in TI with your rotate duration)
(and replace Workspace.Part with the Part’s path/name)

local TI = TweenInfo.new(1,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,-1,false,0)
local RotateTween = game:GetService("TweenService"):Create(workspace.Part,TI,{Rotation = Vector3.new(0,360,0)})

then put these in the said functions:
StartActions.ActionW

RotateTween:Play()

EndActions.ActionW

RotateTween:Pause()

and to make sure all these only fire when our player is seated;
replace

if string.find("W A S D",input.Keycode.Name) then

with

if game.Players.LocalPlayer.Character.Humanoid.SeatPart ~= nil and string.find("W A S D",input.KeyCode.Name) then
1 Like

i understand the last part but i dont understand the first part
what TI and TweenInfo mean?

TI is the abbreviation of TweenInfo

TweenInfo is data used for making tweens, it determines speed of the tween, the repeat amounts, etc…


idk why this keeps happening

it works fine in EndActions

I accidently left the name as ‘RemoteEvent’ there, can you replace it with ‘TurretRemote’?

1 Like

is the IT works only for one RotateTween?
i was trying to make it rotate to the other side

local TI = TweenInfo.new(10,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,-1,false,0)
local RotateTween  = game:GetService("TweenService"):Create(workspace.Part,TI,{Rotation = Vector3.new(0,360,0)})
local Rotate = game:GetService("TweenService"):Create(workspace.Part,TI,{Rotation = Vector3.new(0,-360,0)})

its giving me error in (local rotate)

tweeninfos can be (and should be) reused, and it seems alright to me…

Can you send the error in the output?

Also you might wanna name it something a bit more distinct from RotateTween

i fixed the error but when i press W it works fine , i press s it works fine
but when i try to press W again it stops rotating