[SOLVED] Need help with Weapon collision

  1. What do you want to achieve?
    I want to make the viewmodel have contact with the wall its colliding with, without turning on cancollide as it affects the camera, An old friend lent me his code that he also used to work with, But he is now very inactive and i can’t get help.

  2. What is the issue?

  3. What solutions have you tried so far?
    I tried asking another friend, or whoever’s experienced with these, but he also doesnt know the answer

This is my code:

function ViewmodelCollisions(dt) 
	local CollisionCFrame = CFrame.new()
	local origin = Tool.Handle
	local direction = Camera.CFrame.LookVector

	local params = RaycastParams.new()
	params.IgnoreWater = true
	params.FilterType = Enum.RaycastFilterType.Blacklist
	local FilterParams = {}
	for i,v in next, origin:GetChildren() do
		if v:IsA("MeshPart") then
			table.insert(FilterParams, v)
		end
	end
	for i,v in next, Character:GetChildren() do
		if v:IsA("Hat") or v:IsA("Accessory") then
			table.insert(FilterParams, v )

		end
	end
	params.FilterDescendantsInstances = {FilterParams, Camera:FindFirstChild("Arms"), Character, Camera}

	local ray = workspace:Raycast(Camera.CFrame.Position, origin.GunFirePoint.WorldPosition - (Camera.CFrame * CollisionCFrame).Position, params);
	local check_distance = (Camera.CFrame.Position - origin.GunFirePoint.WorldPosition).Magnitude
	local lerp_position = 0

	if ray then                            
		lerp_position = 1 - (ray.Distance / check_distance)
		CollisionCFrame = CollisionCFrame:Lerp(CFrame.new(0, 0, (math.min((Camera.CFrame.Position - origin.GunFirePoint.WorldPosition).Magnitude / ray.Distance))), lerp_position)
	else
		CollisionCFrame = CollisionCFrame:Lerp(CFrame.new(), game:GetService("TweenService"):GetValue(2 * dt, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)) -- Error message is on this line, on 2 * dt.
	end

	return CollisionCFrame
end

RunService.RenderStepped:Connect(function(dt)
	if Character and HumanoidRootPart and Humanoid then
		local mouseDelta = UserInputService:GetMouseDelta()
		c1 = c1 > 0.1 and lerp(c1, 0, dt * 11) or 0
		local a = velocitySpring.v
		swingSpring.t = Vector3.new(a.z / 1024 / 32 + a.y / 1024 / 16 + (mouseDelta.y / 10) / 1024 * 3 / 2, a.x / 1024 / 32 + (mouseDelta.x / 10) / 1024 * 3 / 2, -(mouseDelta.x / 10) / 1024 * 3 / 2)
		swingSpring:accelerate(Vector3.new(mouseDelta.y/1024, mouseDelta.x/1024, 0))

		local swayVec = swingSpring.p
		local TSWAY = swayVec.z
		local XSSWY = swayVec.X
		local YSSWY = swayVec.Y
		local Sway = CFrame.Angles(YSSWY,XSSWY,XSSWY)
		local aim = aimSpring.p
		local aim2 = aim2Spring.p
		local relv = CFrame.new().VectorToObjectSpace(HumanoidRootPart.CFrame, HumanoidRootPart.Velocity)
		speedSpring.t = (active and Humanoid.Health > 0) and (Vector3.new(1, 0, 1) * relv).magnitude or 0
		velocitySpring.t = relv
		speed = speedSpring.p
		distance = distance + dt * speedSpring.p
		velocity = velocitySpring.p
		swayt = swayt + dt * swaySpeed.p
		if generalSettings.fpsCam then
			local cf = CFrame.new()
			cf = Camera.CoordinateFrame
			local t = swayt
			local s, d = 0.5 * speed, distance * 6.28318 / 4 * 3 / 4
			local ss = swaySpring.p
			local bobAngles = fromAxisAngle(s * math.cos(d + 2) / 2048, s * math.cos(d / 2) / 2048, s * math.cos(d / 2 + 2) / 4096) * fromAxisAngle(ss * math.cos(2 * t + 2) / 2048, ss * math.cos(2 * t / 2) / 2048, ss * math.cos(2 * t / 2 - 2) / 4096)
			local damageAngles = fromAxisAngle(damageSpring.p)
			local impactAngles = fromAxisAngle(impactSpring.p)
			if generalSettings.camBobble then
				cf = cf * bobAngles
			end
			if generalSettings.camHurt then
				cf = cf * damageAngles
			end
			if generalSettings.camImpact then
				cf = cf * impactAngles
			end
			Camera.CoordinateFrame = cf
		end
		if Humanoid.Health > 0 and Camera and Camera:FindFirstChild("Arms") and Camera.Arms.PrimaryPart then
			if Camera and Camera:FindFirstChild("Arms") and Camera.Arms:FindFirstChild("VMArms") then
				local child = Camera.Arms
				if child and child.Name == "Arms" and Character and LeftArm and RightArm then
					child:WaitForChild("VMArms")["Left Arm"].Transparency = 0
					child:WaitForChild("VMArms")["Right Arm"].Transparency = 0
					if Character:FindFirstChild("Shirt") then
						child:WaitForChild("VMArms")["Left Arm"].Shirt.Texture = Character.Shirt.ShirtTemplate
						child:WaitForChild("VMArms")["Right Arm"].Shirt.Texture = Character.Shirt.ShirtTemplate						
					end
					child:WaitForChild("VMArms")["Left Arm"].Color = LeftArm.Color
					child:WaitForChild("VMArms")["Right Arm"].Color = RightArm.Color
				end
			end
			c2 = lerp(c2, generalSettings.recoilYRotMax * c1, c1 == 0 and dt * 7 or dt * 9) + lerp(c3, randomize(2) * c1, c1 == 0 and dt * 7 or dt / 2)
			c3 = lerp(c3, randomize(1.3) * c1, c1 == 0 and dt * 7 or dt * 3)
			c4 = lerp(c4, randomize(5) * c1, c1 == 0 and dt * 7 or dt / 3)
			c5 = lerp(c5, generalSettings.recoilYPosMax * c1, c1 == 0 and dt * 7 or dt * 0.6)
			c6 = lerp(c6, generalSettings.recoilZPosMax * c1, c1 == 0 and dt * 7 or dt * 0.9)
			c7 = CFrame.new(0, c5, c6) * CFrame.Angles(math.rad(c2), c3, c4)

			local cf = CFrame.new()
			cf = Camera.CoordinateFrame * CFrame.new(0, 0, (Camera.FieldOfView / 90 - 1) * 0.3)

			if Camera.Arms:FindFirstChild("isAiming").Value and not Camera.Arms:FindFirstChild("Aim2").Value then
				mouseDelta = mouseDelta * 0.75
				aimSpring.t = Camera.Arms:FindFirstChild("aimSteady").aim.Value
			else
				aimSpring.t = Camera.Arms:FindFirstChild("aimSteady").noAim.Value
			end
			if Camera.Arms:FindFirstChild("Aim2").Value and Camera.Arms:FindFirstChild("isAiming").Value then
				mouseDelta = mouseDelta * 0.75
				aim2Spring.t = Camera.Arms:FindFirstChild("aimSteady").aim.Value
			else
				aim2Spring.t = Camera.Arms:FindFirstChild("aimSteady").noAim.Value
			end
			if generalSettings.canSwing then
				cf = cf * fromAxisAngle(swingSpring.v)
			end
			if Camera.Arms:FindFirstChild("isAiming").Value and not Camera.Arms:FindFirstChild("Aim2").Value then
				if generalSettings.canBobble then
					cf = cf * gunBob(0.7 - 0.3 * aimSpring.p, 1 - 0.8 * aimSpring.p, Humanoid.WalkSpeed) * gunSway(aim)
				end
			elseif Camera.Arms:FindFirstChild("Aim2").Value and Camera.Arms:FindFirstChild("isAiming").Value then
				if generalSettings.canBobble then
					cf = cf * gunBob(0.7 - 0.3 * aim2Spring.p, 1 - 0.8 * aim2Spring.p, Humanoid.WalkSpeed) * gunSway(aim2)

				end
			else
				if generalSettings.canBobble then
					cf = cf * gunBob(0.7 - 0.3 * 0, 1 - 0.8 * 0, Humanoid.WalkSpeed) * gunSway(aim)
				end
			end

			if generalSettings.canAim then
				if Camera.Arms:FindFirstChild("AimPoint") then
					local aimcf = interpolator(aimCFrame)
					cf = cf * aimcf(aim)
				end
			end
			if generalSettings.canAim then
				if Camera.Arms:FindFirstChild("AimPoint2") then
					local aim2cf = interpolator(aim2CFrame)
					cf = cf * aim2cf(aim2)
				end
			end
			if generalSettings.canRecoil then
				cf = cf * c7
			end
			local offset = require(Camera.Arms:FindFirstChild("offset"))
			local offst = interpolator(offset)
			cf = cf * offst(1 - aim - aim2)
			
			cf = cf * ViewmodelCollisions()

			Camera.Arms.PrimaryPart.Anchored = true
			if Camera.CameraType == Enum.CameraType.Custom then
				Camera.Arms.PrimaryPart.CFrame = cf
			end
		end

	end

end)
-- Other functions are just not displayed here.

In my visuals, it seems that the script doesnt know what “dt” is, neither do i (as this code is by a friend)
Any help is appreciated, Cheers!

The function “ViewModelCollisions” requires a ‘dt’ parameter (by the way, its full name is ‘DeltaTime’ and it’s retrieved with RunService events to know how many seconds passed from the last frame) which isn’t being passed.

Just pass it when calling the function and the script shouldn’t break anymore.

1 Like

Just insert dt in the ViewmodelCollisions() brackets inside RenderStepped and everything will work. This way the function will get deltatime from RenderStepped. I hope you understood me

function ViewmodelCollisions(dt) 
	local CollisionCFrame = CFrame.new()
	local origin = Tool.Handle
	local direction = Camera.CFrame.LookVector

	local params = RaycastParams.new()
	params.IgnoreWater = true
	params.FilterType = Enum.RaycastFilterType.Blacklist
	local FilterParams = {}
	for i,v in next, origin:GetChildren() do
		if v:IsA("MeshPart") then
			table.insert(FilterParams, v)
		end
	end
	for i,v in next, Character:GetChildren() do
		if v:IsA("Hat") or v:IsA("Accessory") then
			table.insert(FilterParams, v )

		end
	end
	params.FilterDescendantsInstances = {FilterParams, Camera:FindFirstChild("Arms"), Character, Camera}

	local ray = workspace:Raycast(Camera.CFrame.Position, origin.GunFirePoint.WorldPosition - (Camera.CFrame * CollisionCFrame).Position, params);
	local check_distance = (Camera.CFrame.Position - origin.GunFirePoint.WorldPosition).Magnitude
	local lerp_position = 0

	if ray then                            
		lerp_position = 1 - (ray.Distance / check_distance)
		CollisionCFrame = CollisionCFrame:Lerp(CFrame.new(0, 0, (math.min((Camera.CFrame.Position - origin.GunFirePoint.WorldPosition).Magnitude / ray.Distance))), lerp_position)
	else
		CollisionCFrame = CollisionCFrame:Lerp(CFrame.new(), game:GetService("TweenService"):GetValue(2 * dt, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)) -- Error message is on this line, on 2 * dt.
	end

	return CollisionCFrame
end
RunService.RenderStepped:Connect(function(dt)
	cf = cf * ViewmodelCollisions(dt)
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.