-
What do you want to achieve? Keep it simple and clear!
Firing a bullet from the gun -
What is the issue? Include screenshots / videos if possible!
I have a tutorial script and my own script
I have narrowed down that the tutorial script can fire my premade bullet just fine but my script for some reason has a problem that the bullet not only gets stuck at the barrel and stays there but is also fired at the same time. And if fired multiple times there is a strange effect I can describe.
My script
https://gyazo.com/6f744cf8b886b8b6af9d805b62b56a95
Tutorial script
https://gyazo.com/26bf22adc76a8745b021dde91a39a736
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Ive tried replacing the tutorial scripts into my model and have determined it is not the problem of my model but the actual script. I have tried looking closely at the script contents but they have little to no variation so I dont know what im doing wrong and I really need help. I want to be able to do this on my own without having to use a tutorial script.
Tutorial server script
local tool = script.Parent
local fireEvent = game.ReplicatedStorage.TSFWeapons.ServerEvents.GunFired
local FastCast = require(tool.FastCastRedux)
local firePoint = script.Parent.Barrel30.FirePoint30
local bulletsFolder = workspace:FindFirstChild("BulletFolder") or Instance.new("Folder", workspace)
bulletsFolder.Name = "BulletFolder"
local bulletTemplate = game.Workspace.Bullets.Bullet30
--[[bulletTemplate.Anchored = true
bulletTemplate.CanCollide = false
bulletTemplate.Shape = "Ball"
bulletTemplate.Size = Vector3.new(1, 1, 1)
bulletTemplate.Material = Enum.Material.Metal]]
--FastCast.VisualizeCasts = true
local caster = FastCast.new()
local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Blacklist
castParams.IgnoreWater = true
local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, -workspace.Gravity, 0)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletTemplate = bulletTemplate
local function onEquipped()
castParams.FilterDescendantsInstances = {tool.Parent, bulletsFolder}
end
local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
if bullet then
local bulletLength = bullet.Size.Z/2
local offset = CFrame.new(0, 0, -(length - bulletLength))
bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)
end
end
--[[local function onRayHit(cast, result, velocity, bullet)
local hit = result.Instance
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
character.Humanoid:TakeDamage(50)
end
game:GetService("Debris"):AddItem(bullet, 2)
end]]
local function fire(player, mousePosition)
local origin = firePoint.WorldPosition
local direction = (mousePosition - origin).Unit
caster:Fire(origin, direction, 1000, castBehavior)
end
fireEvent.OnServerEvent:Connect(fire)
tool.Equipped:Connect(onEquipped)
caster.LengthChanged:Connect(onLengthChanged)
--caster.RayHit:Connect(onRayHit)
My server script
local RS = game:GetService("ReplicatedStorage")
local GunFired = RS.TSFWeapons.ServerEvents.GunFired
local gun = script.Parent
local Spawn30 = script.Parent.Barrel30.FirePoint30
local Spawn120 = script.Parent.Barrel120.FirePoint120
local FastCast = require(gun.FastCastRedux)
local bulletsFolder = workspace:FindFirstChild("Bullets") or Instance.new("Folder", workspace)
local bullet30 = bulletsFolder.Bullet30
--local bullet120 = bullets.Bullet120
local Caster = FastCast.new()
--FastCast.VisualizeCasts = true
local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Blacklist
CastParams.IgnoreWater = true
local CastBehaviour = FastCast.newBehavior()
CastBehaviour.RaycastParams = CastParams
CastBehaviour.Acceleration = Vector3.new(0,-workspace.Gravity,0)
CastBehaviour.AutoIgnoreContainer = true
CastBehaviour.HighFidelityBehavior = FastCast.HighFidelityBehavior.Default
CastBehaviour.CosmeticBulletContainer = bulletsFolder
CastBehaviour.CosmeticBulletTemplate = bullet30
local function onEquipped()
CastParams.FilterDescendantsInstances = {gun.Parent, bulletsFolder}
end
local function onLengthChange(cast, lastPoint, direction, length, velocity, bullet)
if bullet30 then
local bulletLength = bullet30.Size.Z/2
local offset = CFrame.new(0,0, - (length - bulletLength))
bullet30.CFrame = CFrame.lookAt(lastPoint,lastPoint + direction):ToWorldSpace(offset)
end
end
local function fire30(player, mousePos)
local origin = Spawn30.WorldPosition
local direction = (mousePos - origin).Unit
print("Fired at ",mousePos)
Caster:Fire(origin,direction,100, CastBehaviour)
end
GunFired.OnServerEvent:Connect(fire30)
gun.Equipped:Connect(onEquipped)
Caster.LengthChanged:Connect(onLengthChange)
scripts have been modified to fire the same bullet. The hit detection has been removed to prove that this not a hit detection issue.