I’m making a gun in roblox studio but the bullets aren’t anchored and CanCollide is set to false because I am using the object LinearVelocity.
But it get deleted after few seconds. How can I fix this ?
I looked on the internet and found nothing, I tried debugging but I couldn’t patch the problem.
I think the problem come from roblox, if you have a CanCollide part false and not anchored, it get destroy after few seconds but i want to disable that
Yes, careful it’s not optimised or very readable because it’s only basics test, there are also few bugs.
Here it is :
local RP = game.ReplicatedStorage
local Players = game:GetService("Players")
local FireEvent = RP.Tools.Gun.Shoot
local Gun = workspace.Tools.Gun
FireEvent.OnServerEvent:Connect(function(player, targetPos)
local Character = player.Character
local ActualGun = Character:FindFirstChild(Gun.Name)
if not ActualGun then return end
local CurrentAmmo = ActualGun:GetAttribute("Ammo")
if CurrentAmmo == 0 then print("out of ammo") return end
local BulletSpeed = 1
--Actual logic :
print("mouse hit : ",targetPos)
local Bullet = RP.Tools.Gun.BulletModel:Clone()
local EndCanon = ActualGun.EndCanon
Bullet.Parent = workspace
local Direction = (targetPos - EndCanon.Position).unit
local Orientation = CFrame.New(Bullet.Position, Bullet.Position + Direction)
Bullet.CFrame = Orientation
Bullet.Position = EndCanon.Position
local Attachment = Instance.new("Attachment", Bullet)
local LinearVelocity = Instance.new("LinearVelocity", Bullet)
LinearVelocity.Attachment0 = Attachment
LinearVelocity.VectorVelocity = Direction * BulletSpeed
LinearVelocity.Enabled = false
--Tests
Bullet.Anchored = false
Bullet.CanCollide = false
--Bullet:Destroy()
print("orientation and direction : ",Orientation," - ",Direction)
end)
The bullets are probably falling through the world and hitting the deletion zone before you move them. I can see that the linearvelocity is disabled when you unanchor them and uncancollide them so, theres nothing to hold them in the world. They’re just going to fall because of gravity.
To fix this issue, you can use a BodyPosition object instead of LinearVelocity. BodyPosition will keep the bullet in the game until it hits a target or reaches its destination.
FireEvent.OnServerEvent:Connect(function(player, targetPos)
local Character = player.Character
local ActualGun = Character:FindFirstChild(Gun.Name)
if not ActualGun then return end
local CurrentAmmo = ActualGun:GetAttribute("Ammo")
if CurrentAmmo == 0 then print("out of ammo") return end
local BulletSpeed = 1
--Actual logic :
print("mouse hit : ",targetPos)
local Bullet = RP.Tools.Gun.BulletModel:Clone()
local EndCanon = ActualGun.EndCanon
Bullet.Parent = workspace
local Direction = (targetPos - EndCanon.Position).unit
local Orientation = CFrame.New(Bullet.Position, Bullet.Position Direction)
Bullet.CFrame = Orientation
Bullet.Position = EndCanon.Position
local BodyPos = Instance.new("BodyPosition", Bullet)
BodyPos.Position = targetPos
BodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
Bullet.Anchored = false
Bullet.CanCollide = true
end)
In this code, I replaced the LinearVelocity object with a BodyPosition object. The BodyPosition object will apply a force to the bullet to move it towards the target position. The MaxForce property is set to a very high value to ensure the bullet moves at a fast speed. I also set CanCollide to true so the bullet will interact with other objects in the game.