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

bullets from my gun spawn wrong?

ok so the bullets in my game spawn on the floor, where i click, or a ways away form the part named "Barrel" please help my game relays on those guns hevaly

script.Parent.Shoot.OnServerEvent:Connect(function(player,LookAt)
	local FlukyGun = player.Backpack:FindFirstChild("WizGun") or player.Character:FindFirstChild("WizGun")
	local cframe = FlukyGun.Barrel.CFrame
	local bullet = game.ReplicatedStorage.Lazer:Clone()
	bullet.Parent = script.Parent
	bullet.CFrame = cframe
	bullet.Velocity = LookAt*350
	local fly = Instance.new("BodyForce",bullet)
	fly.Force = Vector3.new(0,bullet:GetMass()*180,0)
	game.Debris:AddItem(bullet,1)
		wait(2)
		bullet:remove()
	end)

and

script.Parent.Shoot.OnServerEvent:Connect(function(player,LookAt)
	local FlukyGun = player.Backpack:FindFirstChild("WizGun") or player.Character:FindFirstChild("WizGun")
	local cframe = FlukyGun.Barrel.CFrame
	local bullet = game.ReplicatedStorage.Lazer:Clone()
	bullet.Parent = script.Parent
	bullet.CFrame = cframe
	bullet.Velocity = LookAt*350
	local fly = Instance.new("BodyForce",bullet)
	fly.Force = Vector3.new(0,bullet:GetMass()*180,0)
	game.Debris:AddItem(bullet,1)
		wait(2)
		bullet:remove()
	end)

the only other things besides those are parts, a remote event (named shoot) and qPerfectionWeld.

1 answer

I recommend using BodyVelocity as it constantly applies a force to the part, as for the Bullet.Velocity I don't think that's your answer either, I also fail to see what you were using bullet:GetMass() for.

Regardless, when making bullets with physics, adding a BodyVelocity is usually a good option, the script would look something like this.

script.Parent.Shoot.OnServerEvent:Connect(function(player,LookAt)
    local FlukyGun = player.Character:FindFirstChild("WizGun") --If it's in the backpack it means the gun is not equipped, which means a bullet shouldn't be fired
    if FlukyGun == nil then return end --this is just to make sure that it is there, if it isn't then the script will return and it won't shoot

    local cframe = FlukyGun.Barrel.CFrame
    local bullet = game.ReplicatedStorage.Lazer:Clone()
    bullet.Parent = script.Parent
    bullet.CFrame = cframe

    local fly = Instance.new("BodyVelocity",bullet)
    fly.Velocity = Lookat*350

    game.Debris:AddItem(bullet,1)-- removes the bullet after a second. change if you'd like.
    -- you shouldn't need bullet:remove(), however the correct statement would be **Bullet:Destroy()**
end)

It fires the bullet in the direction of LookAt, also it's important to note that you're applying LookAt to your Y axis only, which in my experience will only make it fly up or down.

Assuming that LookAt is your Mouse.Hit.LookVector, the script above should work fine. Otherwise you'll need to explain a little better, because I may have misunderstood.

Hope this helps! :)

2 comments

0

it spawns on the part now but now its even more unusable (the bullets spawn but fall to the floor and fly up) ill explain original problem more. the bullets spawn either 2ish studs from the barrel part or on the floor. they fly fine but the spawning is funky! ill continue tinkering your script and the old one

0

update: i swaped your Velocity part with the old one and its working fine enough for now. if the game gets more populer ill look into fixing it more! but thanks. ill put some creds in the discription

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