Trying to create hitbox

Hello! I am making a combat system and one of the things I’m struggling with is the hitbox creation.

I followed a youtube video, and one of the errors popping up is: attempt to perform arithmetic (add) on Vector3 and number. On the line I emphasize later in the script.

This is my first time making a combat system, and I’ve got no idea on how to fix this.

If you can help me, I would greatly appreciate it!

Thank you.

local rs = game.ReplicatedStorage
local events = rs:WaitForChild('Events')
local hitboxevent = events:WaitForChild('HitBox')

local function newHitBox(character, size, offset, damage, linger)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp == nil then
		return
	end
	local weld = Instance.new('WeldConstraint', hrp)
	local hitbox = Instance.new('Part')
	weld.Part0 = hrp
	weld.Part1 = hitbox
	hitbox.CanCollide = false
	hitbox.CanQuery = false
	hitbox.Massless = true
	
	hitbox.Size = size

--error starts on line below
	hitbox.CFrame = hrp.CFrame.LookVector + offset.X + Vector3.new(0, offset.Y)


	hitbox.Parent = character
	
	hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") == nil then
			return
		end
		
		for _, v in pairs(hitbox:GetChildren()) do
			if v:IsA("ObjectValue") then
				if v.Value == hit.Parent then
					return
				end
			end
		end
		
		local hitCounter = Instance.new("ObjectValue", hitbox)
		hitCounter.Value = hit.Parent
		
		hit.Parent.Humanoid:TakeDamage(damage)
	end)
	
	game.Debris:AddItem(hitbox, linger)
end

hitboxevent.OnServerEvent:Connect(function(plr, size, offset, damage, linger)
	newHitBox(plr.Character, size, offset, damage, linger)
end)
1 Like

Where is this script located? On the server? If so, can i see the client and where it is located?

Yes, this is a server script, located in server script service. The client script is in StarterGUI:

local cas = game:GetService("ContextActionService")
local rs = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")

local events = rs:WaitForChild("Events")
local hitboxevent = events:WaitForChild("HitBox")

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")

local leftPunch = animator:LoadAnimation(script:WaitForChild("LeftPunch"))
local rightPunch = animator:LoadAnimation(script:WaitForChild("RightPunch"))
local middlePunch = animator:LoadAnimation(script:WaitForChild("MiddlePunch"))
--local Block = animator:LoadAnimation(script:WaitForChild("Block"))

local currentPunch = 0

local debounce = false

local holdingkey = false

local startfightstuff = false

local function punch()
	local Blocking = character:FindFirstChild("Blocking").Value
	print(Blocking)

	if debounce then return end

	if Blocking then return end

	debounce = true

	if currentPunch == 0 then
		rightPunch:Play()
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(2), math.random(1, 8), 0.3)
		task.wait(0.4)
		debounce = false
	elseif currentPunch == 1 then
		leftPunch:Play()
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(2), math.random(1, 8), 0.3)
		task.wait(0.4)
		debounce = false
	elseif currentPunch == 2 then
		rightPunch:Play()
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(2), math.random(1, 8), 0.3)
		task.wait(0.4)
		debounce = false
	elseif currentPunch == 3 then
		middlePunch:Play()
		hitboxevent:FireServer(Vector3.new(3, 3, 3), Vector3.new(2), math.random(1, 8), 0.3)
		task.wait(0.4)
		debounce = false
	end

	if currentPunch == 3 then
		currentPunch = 0
	else
		currentPunch = 1
	end
end

--[[local function functiontoblock(ready)
	local Blocking = character:FindFirstChild("Blocking").Value
	if ready then
		game.ReplicatedStorage.BlockBool:FireServer(true)
		Block:Play()
	elseif ready == false then
		game.ReplicatedStorage.BlockBool:FireServer(false)
		return
	end
end
]]
cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)
--[[
UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.One then
		startfightstuff = true
		if input.KeyCode == Enum.KeyCode.Space then
			holdingkey = true
			functiontoblock(true)
		end
	end
end)

UIS.InputEnded:Connect(function(input, gpe)
	if gpe then return end
	if input.KeyCode == Enum.KeyCode.Space then
		holdingkey = false
		functiontoblock(false)
	end
end)]]
1 Like

Why is it in starter GUI if theres no GUI Scripts? I think putting it in starterPlayerScripts or CharacterScripts would keep it more organized

In the server script hrp.CFrame.LookVector + offset.X + Vector3.new(0, offset.Y) your literally just doing hrp.CFrame.LookVector + offset.X because the client sends only an x value so Vector3.new(0,offset.Y) is equal to Vector3.new(0,0,0)

Honestly, not trying to be the bottom of the barrel, but I’m not too familiar with CFrame/Vector3 so I can only guess to wrap hrp.CFrame.LookVector into a vector3. Sorry if im wrong

3 Likes

I just like having local scripts in StarterGUI, putting it in starterplayerscripts and startercharacterscripts is just too confusing for me to get the character for some reason.

I tried removing the Vector3 and it appears that the lookVector is a Vector3 already. It appears that the problem is with offset.X because since offset.X is a number, it tries to add that number to the Vector3. Would wrapping offset.X in a Vector3 solve the issue?

Update: I tried wrapping everything in a CFrame as that is what the property wanted. Shows no errors now, but hitbox does not spawn, even when trying with offset.Y aswell.

Any idea why this is happening? Here is my updated code:

local weld = Instance.new('WeldConstraint', hrp)
	local hitbox = Instance.new('Part')
	weld.Part0 = hrp
	weld.Part1 = hitbox
	hitbox.CanCollide = false
	hitbox.CanQuery = false
	hitbox.Massless = true
	
	hitbox.Size = size
	hitbox.CFrame = CFrame.new(hrp.CFrame.LookVector + Vector3.new(offset.X, 0, 0) + Vector3.new(0, offset.Y, 0))
	hitbox.Parent = character

The offset.X returns a number, which is why it is causing an error. Also, the Vector3.new requires 3 arguements, not two. Just do Vector3.new(0, offset.Y,0) to silence.

This can be fixed by converting offset.X into a Vector3 value. This is what it should look like:

--simplified version
hrp.CFrame.LookVector + Vector3.new(offset.X,0,0) + Vector3.new(0,offset.Y,0)
-- most optimal version
hrp.CFrame.LookVector + Vector3.new(offset.X,offset.Y,0)

image

hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector + Vector3.new(offset.X, offset.Y, 0)

I used some of @thegreatdabb ideas. This works now.

1 Like

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