Problem with the bullet in my gun

Hello, I have a problem with a weapon that I am making, the problem is the following:

It seems that the “Bullet” is “dancing”, apparently it happens when a bullet touches the player before.

The code is the following (Server Script):

local tk = {};

script.Parent.Remotes.GunEvent.OnServerEvent:Connect(function(Player, MX, MY, MZ, Speed)
	if (not tk[Player.UserId] or Modules.LuaAssets.Tick() >= tk[Player.UserId]) then
		tk[Player.UserId] = (Modules.LuaAssets.Tick() + Modules.Tools[Tool.Name].Reload_Time)
		local t = {};
		t.Tool = Player.Character:FindFirstChildOfClass("Tool")
		t.Meta = Modules.LuaAssets.v3(MX, MY, MZ);
		t.Inicio = t.Tool.Handle.Position;
		
		t.DTPS = Speed/((t.Inicio-t.Meta).Magnitude*0.5) t.DTPS = t.DTPS > 20 and Speed/60 or t.DTPS;
		t.Vector = t.Inicio;
		t.A = Modules.LuaAssets.Tick();
		t.B = Modules.LuaAssets.Tick();
		
		t.Proyectil = Structs["Proyectil"]:Clone();
		t.Proyectil.Value = t.Inicio;
		t.Proyectil.Parent = Services.RPS["Proyectiles"]

		t.Event = Services.RUN.Heartbeat:Connect(function(step)
			t.A = Modules.LuaAssets.Tick();
			
			t.Part, t.Hit, t.Normal = workspace:FindPartOnRayWithIgnoreList(Modules.LuaAssets.ray(t.Vector, (t.Meta-t.Vector).Unit*t.DTPS), {Tool, Player.Character});
			t.Mag = (t.Vector-t.Hit).Magnitude;
			t.Vector = t.Hit;
			
			if t.A-t.B > 0.1 then
				t.B = Modules.LuaAssets.Tick();
				t.Proyectil.Value = t.Vector;
			end
			
			if t.DTPS < 1 then
				t.DTPS = 1.1
			end
			
			if t.Part or not Player then
				if t.Part:IsA("BasePart") then
					if t.Part.Parent:FindFirstChild("Humanoid") then
					 t.Part.Parent.Humanoid:TakeDamage(Modules.LuaAssets.Math.random(Modules.Tools[Tool.Name].Min_Damage, Modules.LuaAssets.Math.random(Modules.Tools[Tool.Name].Max_Damage)))
					
					 SetSide(t.Inicio, t.Part.Parent)				
					end
					t.Proyectil.Value = t.Vector;
					Services.DBS:AddItem(t.Proyectil, 0.15);
					t.Event:Disconnect();
					t = nil;
				end
			end
		end)
		Services.DBS:AddItem(t.Proyectil, Modules.Tools[Tool.Name].Proyectil_Lifetime)
	end
end)

Local Script:

local t = {};
local c = {};

Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if (not c[Player.UserId] or Modules.LuaAssets.Tick() >= c[Player.UserId]) then
			c[Player.UserId] = (Modules.LuaAssets.Tick() + Modules.Tools[Tool.Name].Reload_Time)
			script.Parent.Remotes.GunEvent:FireServer(Mouse.Hit.p.X, Mouse.Hit.p.Y, Mouse.Hit.p.Z, Modules.Tools[Tool.Name].Proyectil_Speed);
		end
	end)
end)


Services.RPS.Proyectiles.ChildAdded:Connect(function(Child)
	t[Child] = {
	 	Proyectil = Services.RPS.Bullet:Clone();
	};
	t[Child].Proyectil.CFrame = Modules.LuaAssets.cf(Child.Value);
	t[Child].Proyectil.Parent = workspace.Trash;

	t[Child].Loop = Services.RUN.Heartbeat:Connect(function(step)
		t[Child].Proyectil.CFrame = t[Child].Proyectil.CFrame:Lerp(Modules.LuaAssets.cf(Child.Value), step*10);
	end)
end)

Services.RPS.Proyectiles.ChildRemoved:Connect(function(Child)
	t[Child].Loop:Disconnect();
	t[Child].Proyectil:Destroy();
	t[Child] = nil;
end)

t.Proyectil It is a Vector3Value that I create in a Folder called “Projectiles”.

So I was testing to find out why this happens I noticed that it goes with t.Hit, after doing the raycast t.Vector = t.Hit and then t.Proyectil.Value = t.Vector so that happens but i don’t know how to fix it :neutral_face:

I did print (t.hit) and when the Bullet is “dancing” it prints like this:

Does anyone know why this happens? If you would help me solve it, I would really appreciate it, I hope you have understood the problem I have, thanks for reading :slightly_smiling_face:

3 Likes