When Welding My Hitbox To The character's HumanoidRootPart my character cant move

Im trying to make a hitbox using Spatial Query, But i have this really confusing issue. I cant really explain the issue but here is the video of whats happening:

Here is my code:

--// Services
local RunService = game:GetService("RunService")

--// Modules
local Signal = require(script.GoodSignal)

--// Hitbox Settings (you can change these to your liking)
local Color = Color3.fromRGB(255,0,0)
local Transparency = .1
local CanCollide = false
local Anchored = false
local Massless = true
local Material = Enum.Material.ForceField

--// Main
local module = {}
module.__index = module

function module.new()
	local self = setmetatable({}, module)
	
	self.Size = Vector3.new(0,0,0)
	self.CFrame = CFrame.new(0,0,0)
	self.Offset = CFrame.new(0,0,0)
	
	self.Welded = true
	
	self.Touched = Signal.new()
	
	self.Hitlist = {}
	
	self.Connection = nil
	
	self.Box = nil
	
	self.OverlapParams = OverlapParams.new()
	
	return self
end

function module:Visualize()
	local point_type = typeof(self.CFrame)
	
	local point_cframe = nil
	
	if point_type == "Instance" then
		point_cframe = self.CFrame.CFrame
	elseif point_type == "CFrame" then
		point_cframe = self.CFrame
	else
		error("Incorrect CFrame.")
		return
	end
	
	if self.Box then	
		self.Box.CFrame = point_cframe * self.Offset
		
		if point_type == "Instance" and self.Welded then
			point_cframe = self.CFrame
			
			if not self.Box:FindFirstChildOfClass("Weld") then
				local w = Instance.new("Weld", self.Box)
				w.Part0 = point_cframe
				w.Part1 = self.Box
				w.C0 = self.Offset
			end
		end
	else
		self.Box = Instance.new("Part", workspace.Terrain)
		self.Box.CanCollide = CanCollide
		self.Box.Anchored = Anchored
		self.Box.Color = Color
		self.Box.Name = "Visualizer"
		self.Box.Material = Material
		self.Box.Size = self.Size
		self.Box.Massless = Massless
		self.Box.Transparency = .1
		self.Box.Locked = true	
		self.Box.CFrame = point_cframe * self.Offset
	end
end

function module:Cast()
	local partsinbox = workspace:GetPartsInPart(self.Box, self.OverlapParams)
	
	for i, hit in pairs(partsinbox) do
		local Character = hit:FindFirstAncestorOfClass("Model") or hit.Parent
		local Humanoid = Character:FindFirstChildOfClass("Humanoid")
		
		if Character and Humanoid and not table.find(self.Hitlist, Character) then
			table.insert(self.Hitlist, Character)
			
			self.Touched:Fire(hit, Humanoid)
		end
	end
end

function module:Start()
	self.Connection = RunService.Heartbeat:Connect(function()
		self:Visualize()
		self:Cast()
	end)
end

return module

2 Likes

I’m not sure why the part is being parented to the terrain object? It would make more sense to parent it to the character model. The welds are probably keeping the character in place.

1 Like

I parented it to terrain because i dont want it to be inside the character. thats why

2 Likes

There are other folders for that.
As far as not being able to move what about the line:

		self.Box.Anchored = Anchored
1 Like

The Variable Anchored Is Equal to false

1 Like

Yeah, sorry, I just saw that after I posted.
Why are you making variables like that. Seems to dirty up the code a bit.

local Anchored = false

when you can just use the true or false in lines like

		self.Box.Anchored = Anchored
1 Like

Yeah i Changed That. But I send a vid of the welding. Also i can move but it returns me to my old position its soo weird

1 Like

Ah, so it looks like you are setting the Position of the weld to a world Position. I think you need to set the C0 or C1 of the weld to the Position of the player’s RootPart.

1 Like

wdym by that? im not setting it to a world position

	local point_cframe = nil

		point_cframe = self.CFrame.CFrame

				w.Part0 = point_cframe
				w.Part1 = self.Box
				w.C0 = self.Offset

But where are you setting your w.C1?

Are you really setting your w.Part0 part to self.CFrame.CFrame? Is that even a Part? Naming a Part CFrame is going to create a LOT of confusion.

1 Like

Ok, I would try putting it into workspace though, as terrain tends to be locked and anchored, and may not support updating physics/CFrames etc. Though I’m not sure on runtime behaviour if parenting things to it.

So Basically if you give it a instance It will basically say Part.CFrame Because If I give it an instance then the self.CFrame will be the part so self.CFrame.CFrame basically means Part.CFrame Thats all.

Alright So Turns Out i did it incorrect but i fixed it now by doing:

		if not self.Box:FindFirstChildOfClass("WeldConstraint") and point_type == "Instance" then
			point_cframe = self.CFrame
			
			local w = Instance.new("Weld", self.Box)
			w.Part0 = point_cframe
			w.Part1 = self.Box
			w.C0 = self.Offset
		else
			self.Box.CFrame = point_cframe * self.Offset
		end
	else
		self.Box = Instance.new("Part", workspace.Terrain)
		self.Box.Name = "Visualizer"
		self.Box.CanCollide = false
		self.Box.Anchored = false
		self.Box.Massless = true
		self.Box.Size = self.Size
		self.Box.CanTouch = false
		self.Box.Material = Enum.Material.ForceField
		self.Box.Transparency = .1
		self.Box.Color = Color3.fromRGB(255,0,0)
		self.Box.CFrame = point_cframe * self.Offset
		self.Box.Locked = true
	end ```
2 Likes

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