Part seemingly cloning, yet at the same time isn't

Hello again, developers. For my newest project, I need a hitbox to be constantly attached to the player so when they click, they punch and anything caught in that hitbox takes damage. I have this hitbox stored in Replicated Storage, and am attempting to use a sever sided script with a function that runs on player join to clone this part, move it in front of the player and weld it to the humanoid root part.

I have a check to test if the part was properly cloned, and every time, it runs like it was cloned, yet checking explorer shows it’s nowhere to be found. It’s like its existing in some sort of limbo.

Any help would be appreciated. And yes, I know this isn’t the best way to do hitboxes.

Ignore the many code comments, a lot of them are for my team create companions who can’t code.

local PlayerService = game:GetService("Players")

function addPunchHitbox(plr) --Do I really have to explain this?? 
	plr.CharacterAdded:Connect(function(char)
		if char:FindFirstChildOfClass("Humanoid") ~= nil then
			task.wait(2)
			local keepHitboxConstant = Instance.new("Weld")
			local root = char:FindFirstChild("HumanoidRootPart")
			local lookDirection = root.CFrame.LookVector
			local hit = game:GetService("ReplicatedStorage"):FindFirstChild("M1Box"):Clone() --I don't know why, I don't want to know why, I shouldn't have
			--to wonder why, but this is technically more efficient than .ReplicatedStorage.M1Box:Clone(). It's so dang long. Kill me now.
			if hit ~= nil then --check if clone was succesful
				local offset = 0.25 -- studs in front of the player
				hit.CFrame = root.CFrame + lookDirection * offset --place the part [offset] amount of studs in front of the player
				hit.Parent = char --class part as "child" of player, for ease of access and testing purposes
				print('moved i think')
				keepHitboxConstant.Part0 = root -- attach hitbox to the player
				keepHitboxConstant.Part1 = hit --make sure hitbox moves with the player, not the other way around
				print ('attached')
			end
		else --log if not
			print('something went wrong, couldnt clone')
		end
	end)
end

PlayerService.PlayerAdded:Connect(function(plr) --run both functions when a player joins
	addPunchHitbox(plr)
	-- addBaseMoveset(plr) ignore, unused for now
end)```
1 Like

You never parent the weld so it doesn’t actually weld the hitbox and so when you got check it probably already fell into the void and got destroyed. Just a tip about welds, you don’t need to set the hitbox cframe before setting part0 and 1 because welds use C0 and C1 to determine the parts offset

2 Likes

Thanks so much! It worked.

Now I just need to go position this thing properly…

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