Do we have to create a Caster for each individual weapon? I’m trying to make it modular and have client-server a lot quicker. Previously I had client and server scripts similar to your example. I am now just using the client to handle the physical bullets/etc. While server just do invisible check for player collisions. And I am trying to create a single module that can handle all the guns shooting, instead of having every single gun having their own individual scripts.
local Caster = FastCast.new()
function Bullet.Fire(player, weapon, mousePosition, firePoint)
local WeaponFolder = Weapons:FindFirstChild(weapon)
if not WeaponFolder then return end
local Config = WeaponFolder.Weapon:FindFirstChild("Config")
if not Config then return end
local SPEED = Config:GetAttribute("Speed")
local GRAVITY = Config:GetAttribute("Gravity")
BulletTemplate.BrickColor = player.TeamColor -- Set bullet color to team color
-- Set up cast behavior
local CastBehavior = FastCast.newBehavior()
CastBehavior.RayCastParams = CastParams
CastBehavior.Acceleration = Vector3.new(0, -GRAVITY, 0)
CastBehavior.CosmeticBulletContainer = Bullets
CastBehavior.CosmeticBulletTemplate = BulletTemplate
-- Projectile math
local Origin = firePoint
local Direction = (mousePosition - Origin).Unit
Caster:Fire(Origin, Direction, SPEED, CastBehavior) -- Fire shot
Caster.LengthChanged:Connect(OnLengthChanged)
Caster.RayHit:Connect(OnRayHit)
end
This way it can be a million times easier, than having to go through say 100+ guns and all their scripts. I could just tell it what gun they firing from, and get that guns specifics from there.
My problem I then run into what could be a problem from the server. “Shoot” is fired from the client when they click.
-- SERVER
local FastCast = require(Shared.FastCast)
local Caster = FastCast.new()
-- Default raycast parameters
local CastParams = RaycastParams.new()
CastParams.FilterType = Enum.RaycastFilterType.Blacklist
CastParams.IgnoreWater = true
local function OnRayHit(cast, result, velocity, bullet)
local Hit = result.Instance
local HitCharacter = Hit:FindFirstAncestorWhichIsA("Model")
if HitCharacter and HitCharacter:FindFirstChild("Humanoid") then -- Character
HitCharacter.Humanoid:TakeDamage(100)
end
end
local function FireBullet(player, mousePosition, firePoint)
-- Find and set variables later --
local SPEED = 20
local GRAVITY = 300
---------------------------------------------
-- Set up cast behavior
local CastBehavior = FastCast.newBehavior()
CastBehavior.RayCastParams = CastParams
CastBehavior.Acceleration = Vector3.new(0, -GRAVITY, 0)
local Origin = firePoint
local Direction = (mousePosition - Origin).Unit
Caster:Fire(Origin, Direction, SPEED, CastBehavior)
for _, v in pairs(Players:GetPlayers()) do
if v == player then continue end
ReplicateBullet:FireClient(v, mousePosition, firePoint)
end
end
Shoot.OnServerEvent:Connect(FireBullet)
Caster.RayHit:Connect(OnRayHit)
I am then unable to tell on the server who actually fired the specific shot, so players are able to thus shoot themselves 