Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it possible to make bullets using a regular script, not a local script?

Asked by
Xyternal 247 Moderation Voter
3 years ago

So I know that it really easy to script guns and I thought I'd make something like a turret. But then I realized, that I'm going to need a regular script, not a local script to script the turret. Is there any way for me to make a model that shoots when ever mouseclicked? And can you teach me how to make it so the bullet goes in the direction of the mouse? Thank you!

1 answer

Log in to vote
3
Answered by
2_MMZ 1059 Moderation Voter
3 years ago
Edited 3 years ago

You'll need to have your bullet inside of ReplicatedStorage. Make sure it's one singular model/mesh, that way it's easier to script, and you don't have to get the hard parts of moving a model. Next, you'll need to type this script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = game.ReplicatedStorage:WaitForChild("bulletname")
local startPoint = part.CFrame.p -- This will be the firing point basically
local speed = 100

mouse.Button1Down:connect(function()
local lookPoint = mouse.Hit.p
local projectile = Instance.new("Part") -- Creating the projectile (I'm pretty sure you know how to edit it by using Instance.new with a mesh and configuring it.)
    projectile.Parent = workspace
            projectile.CFrame = CFrame.new(startPoint,lookPoint) -- Projectile starts at "startPoint", while looking at "lookPoint"
            mouse.TargetFilter = projectile -- The mouse will ignore the projectile so it doesn't end up above the mouse position   

local BV = Instance.new("BodyVelocity") -- Creating body velocity
    BV.Parent = projectile -- parenting it to the projectile
        BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge) -- Setting max speed
    BV.Velocity = projectile.CFrame.lookVector * speed -- It will fly in the direction the projectile is facing multiplied by the Speed
end)
0
just a question, isn't local player used for local scripts? Xyternal 247 — 3y
0
Technically. But whilst also running this, you can use RemoteEvents to fire a server event through a localscript. Which will basically do the same moving bullet. 2_MMZ 1059 — 3y
0
You can learn more about RemoteEvents here: https://developer.roblox.com/en-us/api-reference/class/RemoteEvent 2_MMZ 1059 — 3y
0
So I'm first gonna have to create a bullet, and add it to restorage? Or will the script create the bullet and all? Second thing, in your preivious comment, you said local player can be used for local scripts. Can it also be used for server scripts? So is this a local, or server script? Xyternal 247 — 3y
View all comments (4 more)
0
This is a LocalScript. You'll use remote events to create :FireServer() through the localscript, which in another script, will receive the fire through OnServerEvent. In that other script, you'll type the code that makes the bullet move, so that way, it's server-sided. 2_MMZ 1059 — 3y
0
If you're wondering what type the other script will be, try both to see which works. 2_MMZ 1059 — 3y
0
well ill do remote event tutorials then Xyternal 247 — 3y
0
it wasn't working. I suspect its because something I did wrong. I sent you photos, plz help. Xyternal 247 — 3y
Ad

Answer this question