Hello Fellow Developers!
Recently, I was following a tutorial on how to make a RayCast gun Using FastCast module
I ran into a problem while following the tutorial, There was a error where it said “Attemp to index nil with 'Instance”
Here is my code:
local fireEvent = tool.FireEvent
local FastCast = require(tool.FastCastRedux)
local firePoint = tool.Handle.FirePoint
local speed = tool:GetAttribute("Speed")
local damage = tool:GetAttribute("Damage")
local bulletsFolder = Instance.new("Folder", workspace) or workspace:FindFirstChild("BulletFolder")
bulletsFolder.Name = "BulletFolder"
local bulletTemplate = Instance.new("Part")
bulletTemplate.Anchored = true
bulletTemplate.CanCollide = false
bulletTemplate.Shape = "Block"
bulletTemplate.Size = Vector3.new(0.2, 0.2, 1)
bulletTemplate.Material = Enum.Material.Neon
bulletTemplate.BrickColor = BrickColor.new("190")
--FastCast.VisualizeCasts = false
local caster = FastCast.new()
local castParams = RaycastParams.new()
castParams.FilterType = Enum.RaycastFilterType.Exclude
castParams.IgnoreWater = false
local castBehavior = FastCast.newBehavior()
castBehavior.RaycastParams = castParams
castBehavior.Acceleration = Vector3.new(0, -workspace.Gravity, 5)
castBehavior.AutoIgnoreContainer = false
castBehavior.CosmeticBulletContainer = bulletsFolder
castBehavior.CosmeticBulletTemplate = bulletTemplate
local function onEquipped()
castParams.FilterDescendantsInstances = {tool.Parent, bulletsFolder}
end
local function onLenghtChanged(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.new -- This is where the main error is happening
local character = hit:FindFirstAncestorWhichIsA("Model")
if character and character:FindFirstChild("Humanoid") then
character.Humanoid:TakeDamage(damage)
end
end
local function fire(player, mousePosition)
local origin = firePoint.WorldPosition
local direction = (mousePosition - origin).Unit
caster:Fire(origin,direction, speed, castBehavior)
end
game:GetService("Debris"):AddItem(bullet, 10) -- Secondary Problem on "bullet"
fireEvent.OnServerEvent:Connect(fire)
tool.Equipped:Connect(onEquipped)
caster.LengthChanged:Connect(onLenghtChanged)
caster.RayHit:Connect(onRayHit())
I also have a secondary problem with the bullet not being able to be removed, saying
“Unknown global ‘bullet’”
helping me with these would be very much appreciated!
- VeilStrife