I’m making a paintball game, and when I shoot the paintball gun, there’s a 1-second delay on the server. I’m casting rays on the server each time the paintball bullet moves forward, and I’m using Heartbeat to make the bullet move (spawn).
Every time I click, the gun moves backwards. You can see when I click, and you can see when the bullet comes out. They are two different timings that should be the same timing.
Here is some of the code :
-- on server event, shoot.
local function castStep()
local function xypos()
spawn(function()
wait(removeWait)
x:Disconnect()
laser:Destroy()
x:Disconnect()
return
end)
local BulletDrop = (.50 / 2 * (tick() - tick()) ^ 2 / math.pi) * 35
local ray = Ray.new(currentPos, currentNormal * (stepDistance))
local hit, pos, norm = workspace:FindPartOnRayWithIgnoreList(ray, {Player.Character,Ignore,workspace.Terrain},false,true)
laser.Size = Vector3.new(.2,.2,2 + (pos - currentPos).Magnitude)
laser.CFrame = CFrame.new(currentPos:Lerp(pos + Vector3.new(0,math.sin(BulletDrop) * math.sin(BulletDrop) / math.pi,0), .5), pos)
local oldPos = currentPos
currentPos = pos
if hit and hit.Parent and hit.Parent:FindFirstChildOfClass('Humanoid') and hit.Parent:FindFirstChildOfClass('Humanoid').Health > 0 and game.Players:FindFirstChild(hit.Parent.Name) then
laser:Destroy()
x:Disconnect()
hit.Parent:FindFirstChildOfClass('Humanoid'):TakeDamage(3e9)
Player.leaderstats.Hits.Value = Player.leaderstats.Hits.Value + 1
data.Hits = data.Hits + 1
print('Hit Humanoid')
return
elseif
hit and hit.Parent and not hit.Parent:FindFirstChildOfClass('Humanoid') then
laser:Destroy()
x:Disconnect()
print('Didnt hit a humanoid')
local Splatter = Instance.new("Part")
Splatter.Parent = workspace.Terrain
Splatter.Massless = true
Splatter.Anchored = true
Splatter.CanCollide = false
Splatter.Material = Enum.Material.Neon
Splatter.Transparency = 0.4
Splatter.Locked = true
Splatter.CastShadow = false
Splatter.Color = laser.Color
Splatter.Size = Vector3.new(0.46, 0.46, 0.05)
Splatter.CFrame = CFrame.new(pos,(pos + norm))
local cln = game.ReplicatedStorage.Particles.Fire:Clone()
cln.Parent = Splatter
cln.Color = ColorSequence.new(laser.Color,Color3.fromRGB(255,255,255))
spawn(function()
wait(.2)
cln:Destroy()
end)
spawn(function()
wait(20)
local tween = game:GetService("TweenService"):Create(Splatter,TweenInfo.new(5,Enum.EasingStyle.Cubic),{Transparency = 1})
tween:Play()
tween.Completed:Wait()
Splatter:Destroy()
end)
--print(norm * 3)
return
end
end
x = game:GetService("RunService").Heartbeat:Connect(xypos)
EDIT 1 :
Here is the game if you want to see the delay yourself