Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Making a Warship game And i cant get the Turrets to face where my mouse is facing?

Hello Im SASPOOO Im new to Scripting and im working on a project calles WarShips i want this to be a well made game but im not thatt good at scripting i just need some help Thanks!

  • SASPOOO

3 comments

0

CFrame.new ( Vector3 pos, Vector3 lookAt )

0

Where 'pos' is the position of the turret, and 'lookAt' is the position fo the mouse ( mouse.Hit ? )

0

Could you include your script?

1 answer

✓ Accepted answer

ok if you want it to always look where your mouse is, use a loop, but for the actual thing that makes it face the mouse target, make a local script in starterplayerscripts with this code

mouse = game.Players.LocalPlayer:GetMouse()
mousePos = mouse.Hit.Position

then use a remote event "study those its useful for sending information over client to server and server to client", And call that remote event from the client and send over the mouse hit position so like this

mouse = game.Players.LocalPlayer:GetMouse()
mousePos = mouse.Hit.Position

game.ReplicatedStorage.RemoveEvent(please rename it to something):FireServer(mousePos)

so know, the turrret needs to listen to this, and when the event is fired, then activate and make the turret face that mous hit position using a paremeter, btw jsut do you know, roblox automatically makes the first paremeter of remove event listeners the player, so even if you only make one paremeter, ALWAYS make the first one plr, because it is the player that sends over the information nand called the remote event. Anyway, basically to do this, you listen to the remote event with the paremeter like this usign a server script in the turret:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, parameter1)

mousePosition = parameter1 (because paremeter 1 is the actual first paremeter so its the position of the mosue hit when the client side script sent over that information.


end)

to make the turret face it, CFrame has a really nice thing of being able to set the position and direction a part is facing, CFrame can have 6 different uses, and one of them is this nice directional input, so dont change the position, so jsut set the positin to the same position, and then for the directional input (the second one, theres only two) is the mouse.Hit.Position which is parameter 1

turret.CFrame = CFrame.new(turret.Position, mousePosition)

done, so the entire server script code will be

and then put that in the turret and do whatever you want because you can loop it if you want it to always face mouse:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, parameter1)

mousePosition = parameter1

turret.CFrame = CFrame.new(turret.Position, mousePosition)

end)

1 comment

0

sorry i write a lot, too much

This discussion is preserved as it was. New answers, comments, and votes are closed.