Script faster in studio than game?

Hello, so I’ve made a combat system with some VFX but for some reason, in the studio, it works as intended but, in-game it works really slow.

Here is an example:
Studio:
https://gyazo.com/9406e490566445ad9674f6a6915466cc
Game:
https://gyazo.com/91b5c41b2d243384bb34b11136a12dcf

Client Script:

Mouse.Button1Down:Connect(function()
	if ready and not Char:FindFirstChildWhichIsA("Tool") and InAMove == false  and InAKeybindMove == false then
		InAMove = true
		InAPunch = true
		-- // Counting
		PunchCount = PunchCount + 1
		-- // Animations & Cooldown
		Remote4:FireServer(PunchCount,Mouse.Hit)
		spawn(function()
		if PunchCount >= 10 then
			PunchCount = 0
		end
		end)
		
		Punch(PunchCount)        
		ready = false
		wait(0.15) -- cooldown
		InAPunch = false	                
		ready = true
		InAMove = false	
	
		end
end) 

Server Script:

game.ReplicatedStorage.remotes.PunchEvent.OnServerEvent:Connect(function(plr,punchcount,Mouse)
	local Char = plr.Character or plr.CharacterAdded:Wait()
	local Cooldown = false
	local HitBoxPart
	local Ready = true
	
	local function HitBox()
		HitBoxPart = Instance.new("Part",workspace)	
		HitBoxPart.CanCollide = false
	
		HitBoxPart.Name = "HitBox"
		HitBoxPart.Size = Vector3.new(6, 6, 6)
		HitBoxPart.Transparency = 1
		HitBoxPart.Anchored = false
		HitBoxPart.CFrame = Char.HumanoidRootPart.CFrame * CFrame.new(0,0,-1)
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = HitBoxPart
		weld.Part1 = Char.HumanoidRootPart
		weld.Parent = HitBoxPart
		
		
		
		HitBoxPart.Touched:Connect(function(h)
			if h.Parent:FindFirstChild('Humanoid') and h.Parent.Name ~= plr.Name and Cooldown == false then
				-- // Hit Confirmation & Hit Information Processing
				game.ReplicatedStorage.remotes.HitConfirmation:FireClient(plr,HitStatus)
				-- // Combat Effects 
				local ehrp = h.Parent:FindFirstChild("HumanoidRootPart")
				----------------------------------------
				local X = math.random(-180,180)
				local Y = math.random(-180,180)
				local Z = math.random(-180,180)

				spawn(function()
				local HBeam = HitBeam:Clone()
				HBeam.Parent = workspace
				HitBeam.CFrame = CFrame.new((ehrp.CFrame * CFrame.new(0, 0, 0)).Position, Mouse.Position) * CFrame.Angles(0, math.rad(0), 0)
				spawn(function()
						for i = 1,50 do wait()
							HBeam.Size = HBeam.Size + Vector3.new(0.15,0.15,0.5)
						HBeam.Transparency = HBeam.Transparency + 0.1	
				end
				end)
				Derbis:AddItem(HBeam,1.2)
					end)
				end
				-- // ---------------
				if not h.Parent:FindFirstChild("Humanoid") then
				return 
				end
				local Enemy = h.Parent.Humanoid
				Enemy:TakeDamage(5)
				spawn(function()
				Enemy.WalkSpeed = 0
				wait(1)
				Enemy.WalkSpeed = 16
				end)
				if game.Players:FindFirstChild(h.Parent.Name) then	
				wait(0)
				Knockback:FireClient(game.Players:FindFirstChild(h.Parent.Name))
				end
				 Cooldown = true
				 wait(0.5)
				 Cooldown = false
		end)
		Derbis:AddItem(HitBoxPart,0.2)	
	end	
	HitBox()
end)

I thought maybe the problem is the hit processing on server… But I am not sure.

Thank you for your time :slight_smile:

Same for my gun-fighting game.
Can’t really help you with that, but i think its because script lag is more bigger if you are in an actual server and not studio.

That happens with my games too. I think it is because in Studio they can run most services locally and do not have to open up a public server (or even a private one). :confused:

Is there actually no way of fixing it…?

Unfortunately, yes. (30 chrss)

Try printing when you send something through a remote event and when you receive something, and also print the values that were sent/received. That way you can know if client-server delay is causing the issue or something in your code.

1 Like

I’ve done what you’ve suggested and this is the result:
Whenever you go at a medium/slow pace, the script seems to detect amazingly.
https://gyazo.com/a5ba0fa21d6e66b7d3514b2e07a74ecb
Although, it’s when you go fast that there are problems.
https://gyazo.com/a5ba0fa21d6e66b7d3514b2e07a74ecb

Any way to fix it? maybe to increase duration of hitbox?

Alright, so I’ve increased the hitbox duration and it seems to work, thank you :slight_smile:

1 Like