Is there a way to make this faster?

CD = false
script.Parent.Activated:Connect(function()
	if CD == false then
		CD = true
		local Hit = nil
		local Bullet = Instance.new("Part")
		Bullet.Size = Vector3.new(0.2,0.2,0.4)
		Bullet.Color = Color3.fromRGB(255, 255, 0)
		Bullet.Material = "Neon"
		Bullet.Massless = true
		Bullet.CanCollide = false
		Bullet.Parent = workspace
		local Force = Instance.new("BodyForce",Bullet)
		local Dir = script.Parent.Handle.Orientation
		local Pos = script.Parent.Handle.Position
		if Dir.Y > 0 then
			Force.Force = Vector3.new(5,2,0)
		elseif Dir.Y < 0 then
			Force.Force = Vector3.new(-5,2,0)
		else
			Bullet:Destroy()
			warn("Error Dir.Y = 0")	
		end
		local Dir = script.Parent.Handle.Orientation
		local Pos = script.Parent.Handle.Position
		if script.Parent.Parent.Humanoid.MoveDirection.X == 0 then
			if Dir.Y > 0 then
				Bullet.CFrame = CFrame.new(Pos.X+0.1,Pos.Y+0.3,50) * CFrame.Angles(0,math.rad(Dir.Y),0)
			elseif Dir.Y < 0 then
				Bullet.CFrame = CFrame.new(Pos.X-0.1,Pos.Y+0.3,50) * CFrame.Angles(0,math.rad(Dir.Y),0)
			end	
		else
			if Dir.Y > 0 then
				Bullet.CFrame = CFrame.new(Pos.X+6,Pos.Y+0.3,50) * CFrame.Angles(0,math.rad(Dir.Y),0)
			elseif Dir.Y < 0 then
				Bullet.CFrame = CFrame.new(Pos.X-6,Pos.Y+0.3,50) * CFrame.Angles(0,math.rad(Dir.Y),0)
			end	
			print("Moving")
		end		
		script.Parent.Handle.FireSound:Play()
		game.Debris:AddItem(Bullet, 5)
		Bullet.Touched:Connect(function(Hit)
			if Hit.Parent then
				if Hit.Parent.Name ~= script.Parent.Parent.Name then
					if Hit.Parent:FindFirstChild("Humanoid") then
						Hit.Parent.Humanoid.Health = Hit.Parent.Humanoid.Health - 20
					end
					Bullet:Destroy()
				end
			end	
		end)
		wait(0.5)
		CD = false
	end	
end)

The code above is for a gun that i have made. It is for a 2D game and the direction it moves is by the direction of the players arm. This is the only script insied the gun. The problem is that it takes like 0.3 seconds for the gun to shoot. I want it to be more instant.

I have litteraly no idea how to make it faster so that is why i went here. Most free model guns that i have tried is way faster and does it almost instantly even tho their code is alot longer and does alot. Mine has no effects or anything like that.

So how can i make this faster?

1 Like

Is this a Script or a LocalScript? If it is a Script there will be latency between when the player clicks and when the Activated event fires.

To fix this, you will need to do these things on the client (in a LocalScript) and then tell the server about it (send it to a Script using a RemoteEvent/Function) – don’t forget to validate on the server so hackers can’t exploit it, though.

Other players won’t be able to see bullets by default if you do this, so to make it so other players can see the bullets, the easiest option will probably be to send each player information about the shot and then get them each to make and show their own bullets.

Example flow:

  1. Player1 shoots their gun at Player2
    a. Bullet is created and fired on the client
    b. Player2’s humanoid is damaged on the client
    c. Client sends this information to the server using a RemoteEvent
  2. Server receives the request to shoot
    a. Validates this request, makes sure the player isn’t hacking
    b. Player2’s humanoid is damaged on the server
    c. Server fires a RemoteEvent to all players except for Player1 with information to create a bullet
  3. All clients (let’s say Player2 in this case) except for the shooter receive information about the bullet
    a. Bullet is created on the client
3 Likes

Is it not just faster to have a localscript detect the mouse and then send it to a regular script via remoteevent? and let the regular script make a bullet then?

Yes that is what I described.

No because making the bullet in a regular script is what is making your gun appear to work slowly. To do this you need to go from client to server, which takes time, so you want to avoid doing that as much as possible so that the player (shooter) can get instant feedback on actions they take. It’s not as important for other players since whatever you do, there will be latency.

Also, creating the bullet in a server script (for the sake of all of the other players, and instead of sending it to each player) presents other issues. Firstly, server-side performance (if it’s tight for your game). Secondly, if the shooter’s client is making its own bullets then how will you handle having two different bullet objects for the same shot on the shooter’s client?

But if i make the bullet in a localscript then only the client will see it. Or do you mean sending info to all clients about the bullet? wont that just take more time? is it not just faster to let the server handle the bullet or am i completely wrong?

2 Likes

i tried it. There is 1 huge problem tho. It only damages for the client. for the server no player gets damaged which is really bad.

1 Like

Yep.

It doesn’t make much of a difference. The server effectively does exactly the same thing, it’s just hidden (instead of using RemoteEvents).

1 Like

Yep that’s why I said in the flow description that you damage the enemy humanoid on the server (as well as the shooter’s client).

1 Like