so ive made a gun in roblox studio and when i test it its perfectly fine,but when i work it in game there is a really big delay between the click and it firing, here are both the server side and client side scripts:
server side
local Gunevent = RS.GunEvent
local TweenService = game:GetService("TweenService")
local debri = game:GetService("Debris")
local particle = RS.ParticlePart
local function aftereffects(start,rayendpos,YN,sound,gun)
sound:Play()
gun.Muzzle.ParticleEmitter:Emit(1)
if YN == true then
local newparticle = particle:Clone()
newparticle.Parent = game.Workspace
newparticle.Position = rayendpos
newparticle.ParticleEmitter:Emit(3)
newparticle.ParticleEmitter.Enabled = false
debri:AddItem(newparticle,0.3)
end
end
local function Damage(Hitpart,player)
local Model = Hitpart:FindFirstAncestorOfClass("Model")
if Model then
print("hithumanoid")
if Model:FindFirstChild("Humanoid") then
if Hitpart.Name == "HumanoidRootPart" then
Hitpart = Model:FindFirstChild("Torso")
for i,v in pairs(Hitpart:GetChildren()) do
if v.ClassName == "NumberValue" and v.Value > 0 then
print("i see")
print(v.Value)
v.Value = v.Value - 1
print(v.Value)
end
end
else if Hitpart.Name ~= "HumanoidRootPart" then
print(Hitpart)
for i,v in pairs(Hitpart:GetChildren()) do
if v.ClassName == "NumberValue" and v.Value > 0 then
print("i see")
print(v.Value)
v.Value = v.Value - 1
print(v.Value)
end
end
end
end
end
end
end
local function RayMaker(distance,RayEndPos,gun)
local p = Instance.new("Part")
p.Name = ("projectile")
p.Parent = game.Workspace
p.Anchored = true
p.CanCollide = false
p.CanQuery = false
p.CanTouch =false
local tweeninfo = TweenInfo.new(0.2,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out,0,false,0)
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.lookAt(gun.Muzzle.Position,RayEndPos)*CFrame.new(0, 0, -distance/2)
p.Material = Enum.Material.Neon
p.BrickColor = BrickColor.new("New Yeller")
local Goals = {Size=Vector3.new(0,0,0),Position = RayEndPos}
local tween = TweenService:Create(p,tweeninfo,Goals)
tween:Play()
debri:AddItem(p,0.2)
end
Gunevent.OnServerEvent:Connect(function(player,instruction,gun,mousehitpos,sound,lookvector,cooldown)
if instruction == "Shoot"then
local RayCastTask = task.spawn(function()
local raycastParams = RaycastParams.new(player.Character:GetDescendants())
raycastParams.FilterDescendantsInstances = {player.Character:GetDescendants()}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local RayCastResult = workspace:Raycast(gun.Muzzle.Position,(mousehitpos - gun.Muzzle.Position) * 300 ,raycastParams)
if RayCastResult then --if shot at something
local RayEndPos = RayCastResult.Position
local distance = (gun.Muzzle.Position - RayEndPos).Magnitude
local hitpart = RayCastResult.Instance
Damage(hitpart,player)
print(gun)
RayMaker(distance,RayEndPos,gun)
aftereffects(lookvector,RayEndPos,true,sound,gun)
Gunevent:FireClient(player, "Recoil",gun)
else if not RayCastResult then --if shoot in void
local RayEndPos = gun.Muzzle.Position + lookvector * 300
local distance = (gun.Muzzle.Position - RayEndPos ).Magnitude
RayMaker(distance,RayEndPos,gun)
aftereffects(lookvector,RayEndPos,false,sound,gun)
Gunevent:FireClient(player, "Recoil",gun)
end
end
end)
end
end)```
client side:
```local uis = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
--local module = require(script.Parent.AnimPlayer)
local RS = game.ReplicatedStorage
local RF = RS.RemoteFunction
local GunEvent = RS.GunEvent
local Players = game:GetService("Players")
local Player = game:GetService("Players").LocalPlayer
print(Player)
local gun = script.Parent
local mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local cameraoffset = 0.1
local DefaultFoV = 70
local FieldOfView = Camera.FieldOfView
local ViewModel = Camera:WaitForChild("ViewModel")
local Offset = ViewModel:WaitForChild("Offset")
local aimcooldown = false
local M2Held = false
local isequipped
local aiming = false
local isrunanimplaying = false
local t
local Mouse = Player:GetMouse()
local Character = Player.Character
if not Character.Parent then Character = Player.CharacterAdded:Wait() end -- // Player.Character can be the old character sometimes.
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")
local weapon = script.Parent
local AimPart = weapon.AimPart
local Handle = weapon.Handle
local HoldAnim = Instance.new("Animation", script.Parent)
HoldAnim.AnimationId = "rbxassetid://12864760596" -- // Please note that you HAVE TO change this ID to your own animation ID since this animation was made by me and will only work if you are playing in my own place.
HoldAnim = Animator:LoadAnimation(HoldAnim)
HoldAnim:SetAttribute("ViewModelAnimation", true)
----------------------------
local function stopaim()
local HandleTransform = ViewModel:GetPrimaryPartCFrame():ToObjectSpace(Handle.CFrame)
local OriginalTransform = CFrame.new()
local AimTransform = AimPart.CFrame:ToObjectSpace(Handle.CFrame) * HandleTransform:Inverse()
for t = 0, 1,0.1 do
if M2Held == true then
break
end
wait()
Offset.Value = AimTransform:Lerp(OriginalTransform,t)
end
end
local function startaim()
local HandleTransform = ViewModel:GetPrimaryPartCFrame():ToObjectSpace(Handle.CFrame)
local OriginalTransform = CFrame.new()
local AimTransform = AimPart.CFrame:ToObjectSpace(Handle.CFrame) * HandleTransform:Inverse()
for t = 0, 1,0.1 do
if M2Held == false then
break
end
wait()
Offset.Value = OriginalTransform:Lerp(AimTransform,t)
end
end
local function zoomer(tf) -- change fov
if tf == true then
local Properties = {FieldOfView = DefaultFoV - 30}
local Info = TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)
T:Play()
else if tf == false then
local Properties = {FieldOfView = DefaultFoV}
local Info = TweenInfo.new(0.15, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)
T:Play()
end
end
end
local function CrossHair(tf)
if tf == "true" then
Player.PlayerGui.Gungui.CrosshairFrame.Visible = true
uis.MouseIconEnabled = false
else if tf == "false" then
Player.PlayerGui.Gungui.CrosshairFrame.Visible = false
uis.MouseIconEnabled = true
else if tf == "tralse" then
Player.PlayerGui.Gungui.CrosshairFrame.Visible = false
uis.MouseIconEnabled = false
end
end end
end
-----------------------
---------------------------------------------------
Mouse.Button2Down:Connect(function() -- start aim
M2Held = true
end)
Mouse.Button2Up:Connect(function() -- end aim
M2Held = false
end)
---------------
gun.Equipped:Connect(function() -- gun equipped
isequipped = true
HoldAnim:Play()
end)
gun.Unequipped:Connect(function() -- gun unequipped
isequipped = false
CrossHair("false")
HoldAnim:Stop()
end)
RunService.RenderStepped:Connect(function() -- aiming handling
local dist = (workspace.CurrentCamera.CFrame.Position - Character.Head.Position).Magnitude
if M2Held == false and aiming == true then
aiming = false
stopaim()
else if M2Held == true and aiming == false then
aiming = true
startaim()
end
end
if isequipped == true and aiming == false then
CrossHair("true")
local pos = uis:GetMouseLocation()
Player.PlayerGui.Gungui.CrosshairFrame.Position = UDim2.new(0, pos.X, 0, pos.Y); -- Changes image position to the mouse position
elseif isequipped == true and dist < 1 then
CrossHair("true")
Player.PlayerGui.Gungui.CrosshairFrame.Position = UDim2.new(0,0, 0,0); -- Changes image position to the mouse position
else if isequipped == true and aiming == true and dist < 1 then
CrossHair("tralse")
end
end
end)
----------------------------------------------------------------------------------------
mouse.Button1Down:Connect(function() -- shot
if isequipped == true then
GunEvent:FireServer("Shoot", gun, mouse.Hit.Position,gun["M1911 Gunshot"],mouse.Hit.LookVector,0.75 )
end
end)
mouse.Button1Up:Connect(function() -- nothing yet
end)
----------------------------------------------------------
GunEvent.OnClientEvent:Connect(function(instruction,gun) -- recoil applier
if instruction == "Recoil" then
Camera.CFrame = Camera.CFrame * CFrame.Angles(math.rad(3),0,0)
end
end)```