I am very new to scripting so I'm sorry if this scripts untidiness makes you cringe.
01 | local tool = script.Parent |
02 | local player = tool.Parent.Parent |
03 | local mouse = player:GetMouse() |
04 | local root = player.Character.HumanoidRootPart |
05 |
06 |
07 | tool.RemoteEvent.OnServerEvent:connect( function (player,mousehit) |
08 | if player.Character:FindFirstChild( "multiattackprevention" ) then return end |
09 | local multiattackprevention = Instance.new( "StringValue" ) |
10 | multiattackprevention.Parent = player.Character |
11 | multiattackprevention.Name = ( "multiattackprevention" ) |
12 | local hitcooldown = false |
13 | local animation = player.Character.Humanoid:LoadAnimation(player.Character:FindFirstChild( "SuperJump" )) |
14 | animation:Play() |
15 | wait( 0.5 ) |
This works with no output error, however the projectile doesn't spawn.
The error is to do with:
1 | bv.Velocity = root.CFrame.p * 150 |
I know this because when I put:
1 | bv.Velocity = mousehit.lookVector * 150 |
the projectile fires, just in the wrong direction.
What should I make bv.Velocity equal to?
If you want your projectile to fire in the direction the player's torso is facing, which is what it seems like, you were very close. When you had:
1 | bv.Velocity = root.CFrame.p * 150 |
The projectile DID spawn, it was just moving so quickly you couldn't catch a glimpse of it, because you used position, which is usually a large value, so your projectile went shooting off at the speed of sound. What you want is lookVector, which is the unit vector of the direction a CFrame is facing.
1 | bv.Velocity = root.CFrame.lookVector * 150 |
Enjoy :)
Also, a tip: When using Instance.new(), you can assign a parent instantly, without using another line. Just put a comma, and then what you want to parent the new object to.
1 | local bv = Instance.new( "BodyVelocity" , player.Character.Torso) |
2 |
3 | --Instead of |
4 |
5 | local bv = Instance.new( "BodyVelocity" ) |
6 | bv.Parent = player.Character.Torso |
Here's an idea
1 | projectile.CFrame = CFrame.new(projectile.CFrame,(mouse.hit.p.x,mouse.hit.p.y,mouse.hit.p.z)) |
If you want the BodyVelocity's velocity to "face" your mouse.
1 | Projectile.MaxForce = Vector 3. new( 1 e 8 , 1 e 8 , 1 e 8 ) |
2 | Projectile.Velocity = (mouse.hit.p - root.Position).unit * bulletSpeed |
To make the projectile face your mouse. (Makes the projectile look nicer)
1 | Projectile.CFrame = CFrame.new(Projectile.Position, mouse.hit.p) |
You can get the mouse using the player got from the RemoteEvent.
Here's my gun's script. Don't blame me for untidy lines, I just wanted to make it quick.
001 | --Defines |
002 | local tool = script.Parent |
003 | local fire = tool.Fire |
004 | local remote = tool:WaitForChild( "RemoteEvent" ) |
005 |
006 | --Settings |
007 | local damage = 40 -- Headshot damage is * 2.5 |
008 | local bulletSpeed = 500 -- Don't go for too much please |
009 |
010 | --Startup |
011 | if tool.Parent:IsA( "Model" ) then |
012 | if game.Players:GetPlayerFromCharacter(tool.Parent) then |
013 | for i,v in pairs (tool:GetChildren()) do |
014 | if v:IsA( "Part" ) or v:IsA( "BasePart" ) then |
015 | v.CanCollide = false |
It's unfinished so don't try.