I am making a gun framework for my game
Notes and Script Samples:
- I am raycasting on the server via a seperate module functions
Summary
function GunFunctions.ShootGun(MousePosition, Gun, Character)
if Gun then
-- Physical Gun Variables --
local GunModel = Character:FindFirstChild(Gun)
local FirePoint = Character:FindFirstChild(Gun):FindFirstChildWhichIsA("Model").FirePoint
-- Gun Stats --
local Range = GunInformationTable[Gun]["Range"]
local Damage = GunInformationTable[Gun]["Damage"]
local FireRate = GunInformationTable[Gun]["Fire Rate"]
local Offset = GunInformationTable[Gun]["Spread"]
local GunSound = GunInformationTable[Gun]["Gun Shoot"]
local RandomFunc = Random.new()
local RandomOffset = Vector3.new(RandomFunc:NextNumber(-Offset, Offset), RandomFunc:NextNumber(-Offset, Offset), RandomFunc:NextNumber(-Offset, Offset))
MousePosition += RandomOffset
-- Raycasting --
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = { Character , GunModel:GetChildren() }
Params.FilterType = Enum.RaycastFilterType.Exclude
local Origin = FirePoint.Position
local Direction = (MousePosition - Origin).Unit * Range
local RayCast = workspace:Raycast(Origin, Direction, Params)
ReplicatedStorage.GunRemote:FireAllClients("Effects", FirePoint, Direction, GunSound, RayCast)
if RayCast then
local InstancedResult = RayCast.Instance
if InstancedResult then
if InstancedResult.Parent:FindFirstChild("Humanoid") then
local Humanoid = InstancedResult.Parent:FindFirstChild("Humanoid")
for i, v in pairs(Damage) do
if tostring(InstancedResult) == i then
Humanoid:TakeDamage(v)
end
end
else
--print("not humanoid")
--print(InstancedResult)
ReplicatedStorage.GunRemote:FireAllClients("Bullet Holes", nil, nil, nil, InstancedResult, RayCast.Position, RayCast.Normal)
end
end
end
end
end
- Effects are on the client using :FireAllClients
Summary
GunRemote.OnClientEvent:Connect(function(Purpose, FirePoint, Direction, ShootSound, InstancedResult, RayPos, RayNorm)
if Purpose == "Effects" then
if not FirePoint:FindFirstChild("MuzzleFlash") then
for i, v in pairs(ReplicatedStorage.GunFx.MuzzleFlash:GetChildren()) do
local Particle = v:Clone()
Particle.Parent = FirePoint
if Particle:IsA("SpotLight") then
Particle.Enabled = true
end
Debris:AddItem(Particle, .15)
end
if not FirePoint:FindFirstChild("GunSmokeEffect") then
local GunSmoke = ReplicatedStorage.GunFx.GunSmoke.GunSmokeEffect:Clone()
GunSmoke.Parent = FirePoint
Debris:AddItem(GunSmoke, 3)
end
end
--[[
local midpoint = FirePoint.Position + Direction/2
local VisibleRay = Instance.new("Part", workspace)
VisibleRay.Name = "Bullet"
VisibleRay.Anchored = true
VisibleRay.CanCollide = false
VisibleRay.Material = Enum.Material.Neon
VisibleRay.BrickColor = BrickColor.new("Yellow flip/flop")
VisibleRay.CFrame = CFrame.new(midpoint, FirePoint.Position)
VisibleRay.Size = Vector3.new(.1,.1, Direction.Magnitude)
Debris:AddItem(VisibleRay, .5)]]
------------------------------------------------------------
local GunShotSound = Instance.new("Sound", FirePoint)
GunShotSound.SoundId = ShootSound
GunShotSound:Play()
Debris:AddItem(GunShotSound, 1)
elseif Purpose == "Bullet Holes" then
local ImagePart = Instance.new("Part", workspace.BulletHoles)
ImagePart.Transparency = 1
ImagePart.CanCollide = false
ImagePart.Anchored = true
ImagePart.Size = Vector3.new( .5, .5, .5 )
ImagePart.CFrame = CFrame.new( RayPos, RayPos + RayNorm)
local BulletImage = Instance.new("Decal", ImagePart)
BulletImage.Texture = bulletHoleImageIdsBasedOnMaterial[InstancedResult.Material] or ""
BulletImage.Face = Enum.NormalId.Front
Debris:AddItem(ImagePart, 10)
--
elseif Purpose == "Reload" then
-- print("firing clients, reloading")
local GunReload = Instance.new("Sound", FirePoint)
GunReload.SoundId = "rbxassetid://8245961296"
GunReload:Play()
Debris:AddItem(GunReload, 1)
end
end)
Issue: I have been developing this framework for the past 3 days, and latency/lag wise, there has been absolutely 0 issues. That is however, until I opened my game and told my friends to join to do some stress testing, just incase, also for the purpose of some constructive critiscm.
Comparisons and Clips below TW: Gun shots, they are loud I advise turning down your volume
In Roblox Studio: testing, the guns work amazing; there are no lag issues, no frame drops, everything works how it is supposed to
In Main Roblox App: Latency is beyond horrendous, raycasts take forever to fire, or they literally never do. My guns have a bullet spread system so guns are not 100% accurate, at first I thought the reason nobody was hitting anything was for this( I thought I had to tweak the spread system). This was NOT the issue as I went in roblox studio right afterwards, keeping the same values and it was fine. The raycasts just take forever to fire. The script rate isn’t even high.
Main Roblox App 1
Main roblox App 2
Also: Even when I am in the main roblox game, even by myself with nobody else in the server it still lags
I need help with this ASAP, I’ve never had to deal with these type of problems.