Client projectiles flying faster on faster pc's

In studio my projectile runs perfectly at the right speed, when I go into game it flys super fast for me my pc is pretty good and for my friend it fly’s medium speed on their laptop. It kinda varies depending on the speed of your computer, I’m not using tween service because I want the zig zag effect so it looks like a bolt of energy so instead I’m using a while wait do to move the projectile.

studio speed/the speed it should be

speed in-game

You should provide some code, but assuming you’re using RunService.Stepped to move your projectile, utilize the time delta arguments provided to scale the distance your projectiles travel:

local studsPerSecond = 40

game:GetService("RunService").Stepped:Connect(function(totalDuration, timeSinceLastFrame)
    local amountToMoveThisFrame = studsPerSecond * timeSinceLastFrame

    MoveBulletForward(amountToMoveThisFrame)
end)

note: Heartbeat is similar, but you omit the totalDuration argument

I’m using heartbeat although i’m not entirely sure how to use it properly.

		while wait do
			game["Run Service"].Heartbeat:Wait()
			local ray = Ray.new(orgin,Cframe.LookVector*speed/30)
			hit,hitpos,normal = game.Workspace:FindPartOnRayWithIgnoreList(ray,ignore)
			p.Position = hitpos + Vector3.new(math.random(0,offset),math.random(0,offset),0)
			orgin = hitpos
			
			if (Cframe.Position - p.Position).magnitude >= range then
				p.Anchored = false
				game:GetService("Debris"):AddItem(p,.1)
				break
				
			end
			
			--if hit
			if hit then
				local Hum = hit.Parent:FindFirstChild("Humanoid")
				local HitPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			
				HitFunction({p=p,Hum = Hum,Player = HitPlayer})
				
				--burn marks
				local Hole = RepStorage.Effects.Hole:Clone()
				Hole.Parent = game.Workspace.SpellEffects
				Hole.Position = hitpos
				Hole.CFrame = CFrame.new(Hole.Position, Hole.Position+normal)
				
				--hit sound
				p.Hit:Play()
				
				--delete spell
				p.Anchored = true
				handleHit({hit = hit,hitpos = hitpos,p = p,Hole = Hole})
				break
			end
		end
  1. Why is there a space in ["Run Service"]? Does this code actually work? Did you write it?
  2. Just replace while wait do with while true do. “wait” is a function.
  3. If you want to use the :Wait() version of events that’s fine (although I recommend the :Connect version), you can still get the arguments from the return value:
-- ...
while true do
    local timeSinceLastFrame = game:GetService("RunService").Heartbeat:Wait()
    local ray = Ray.new(orgin, Cframe.LookVector * speed * timeSinceLastFrame)
-- ...
1 Like

I get a really bad lag spike, in the micro profiler you can see it goes up to around 80. Is there a way to stop that?

		while true do
			local timeSinceLastFrame = game:GetService("RunService").Heartbeat:Wait()
   			local ray = Ray.new(orgin, Cframe.LookVector * speed * timeSinceLastFrame)
			--local ray = Ray.new(orgin,Cframe.LookVector*speed/30)
			hit,hitpos,normal = game.Workspace:FindPartOnRayWithIgnoreList(ray,ignore)
			p.Position = hitpos + Vector3.new(math.random(0,offset),math.random(0,offset),0)
			orgin = hitpos
			
			if (Cframe.Position - p.Position).magnitude >= range then
				p.Anchored = false
				game:GetService("Debris"):AddItem(p,.1)
				break
				
			end
			
			--if hit
			if hit then
				local Hum = hit.Parent:FindFirstChild("Humanoid")
				local HitPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			
				HitFunction({p=p,Hum = Hum,Player = HitPlayer})
				
				--burn marks
				local Hole = RepStorage.Effects.Hole:Clone()
				Hole.Parent = game.Workspace.SpellEffects
				Hole.Position = hitpos
				Hole.CFrame = CFrame.new(Hole.Position, Hole.Position+normal)
				
				--hit sound
				p.Hit:Play()
				
				--delete spell
				p.Anchored = true
				handleHit({hit = hit,hitpos = hitpos,p = p,Hole = Hole})
				break
			end
		end

Well, because its a spike confined to the start of the shot, it doesn’t seem like the problem is in the loop to me. It seems like the problem is with however you’re instantiating the shot.

Something like this (PartCache, for all your quick part-creation needs) might help, but you should really use some custom profiling to figure out what the problem is.

1 Like