Need help understanding something in my own code


ima be honest, i was trying to make a script for a baseball bat weapon, got most of my code from a few youtube videos, and stitched them together

can someone help me understand whats happening in line 20? if you want to find the specific line quickly, i left a pretty big comment on the line im talking about. im trying to fix a bug thats caused by whatever is going on in that line of code but if i cant understand whats happening then i cant fix it

if theres anymore info you need (ex. ragdoll module script code, etc…) i can add them

(the script is located inside of the Tool object for the weapon)

local tool = script.Parent
local bat = tool.BatModel
local damage_re = tool:WaitForChild("SwingEvent")
local hitsound = bat.BatHit
local ragdollmodule = require(game:GetService("ServerScriptService").Ragdoll)
local kbmodule = require(game:GetService("ServerScriptService").Knockback)

local hitbox = Instance.new("Part",tool)

local connection = nil

local hitlist = {}

local function inflict_damage(other_object)
	if(not hitlist[other_object.Parent]) then 
		local humanoid = other_object.Parent:FindFirstChild("Humanoid")
		if humanoid then
			hitlist[other_object.Parent] = true
			humanoid:TakeDamage(25)
			kbmodule.Start(humanoid.Parent, (humanoid.Parent.HumanoidRootPart.Position - other_object.Position).Unit * Vector3.new(60,40,60) + Vector3.new(0,40)) --THIS IS THE LINE I DONT UNDERSTAND, the parameters for this function are (character,direction)
			ragdollmodule.Start(humanoid.Parent)
			for i in hitlist do
				hitsound:Play()
			end
			wait(2)
			ragdollmodule.Stop(humanoid.Parent)
		end
	end
end

damage_re.OnServerEvent:Connect(function()
	local hitbox = Instance.new("Part",tool)
	hitbox.Size = Vector3.new(1,5,1)
	hitbox.CanCollide = false
	hitbox.Massless = true
	hitbox.Transparency = 0.7 --debug

	local weld = Instance.new("Weld",hitbox)
	weld.Part0 = bat
	weld.Part1 = hitbox
	game.Debris:AddItem(hitbox,0.4)

	hitlist = {}
	
	connection = hitbox.Touched:Connect(inflict_damage)
end)

since the bug seems to be happening with the kbmodule thing, heres also the module script code for knockback in case it has something to do with that

(located in server script service)

local kb = {}

function kb.Start(character,direction)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	
	if hrp == nil then return end
	
	local att = Instance.new("Attachment",hrp)
	local lv = Instance.new("LinearVelocity",att)
	lv.MaxForce = 9999999
	lv.VectorVelocity = direction
	lv.Attachment0 = att
	game.Debris:AddItem(att,0.1)
end

return kb
2 Likes

did you completely watch the videos or did you just take the code?

What’s going on in that line is the script sends the required arguments character which I assume is the character of the player that’s getting launched away, and the direction that they are getting launched towards.

it first passes humanoid.Parent, which is the player that is being launched. It then passes on the direction. The direction is calculated by getting the difference between two vectors and then creating a unit vector that gives us the direction that the person getting launched is going towards, being the position of their root part and “other_object”.

Also I’m not too sure about why the direction is multiplied by 60x, 40y, 60z + 0x, 40y, so could you print (humanoid.Parent.HumanoidRootPart.Position - other_object.Position).Unit * Vector3.new(60,40,60) + Vector3.new(0,40) before you damage the humanoid?

Also what’s the bug exactly?

1 Like

Alright, I will be direct in the answer.

kbmodule.Start(humanoid.Parent, (humanoid.Parent.HumanoidRootPart.Position - other_object.Position).Unit * Vector3.new(60,40,60) + Vector3.new(0,40)) --THIS IS THE LINE I DONT UNDERSTAND, the parameters for this function are (character,direction)

Alright I’m gonna break this up into the component parts to make this understandable cuz lets be honest this code is ROUGH to read.


local Character = humanoid.Parent
local CharacterPositionV3 = Character.HumanoidRootPart.Position 
local TargetPositionV3 = other_object.Position

-- This is kind of strange as to why not just make it 60, 80, 60 to start with but ok 
-- What is happening here is once you get the direction that you want, you are functionally giving it a magnitude 
-- Which will then create a velocity which is a direction with a magnitude. 
-- Likely this is some wierd way to control the direction of the magnitude and thats why its done this way unsure 
-- but thats waht this value is
local CONST_POWER = (Vector3.new(60,40,60) +Vector3.new(0,40))

--[[
So when you have a vector, when you subtract it the FIRST vector is direction FROM the second vector is directiuon TOO
So CharacterPos - TargetPos is getting the Direction from CharacterPos To the Target.   We unitize it to make it have Zero 
magnatude. So its an Abstract direction with no position Data for it. 
]]
local function _getUnitizedDirectionOfObjectToHit(CharacterPositionV3, TargetPositionV3)
	return (CharacterPositionV3 - TargetPositionV3).Unit
end

kbmodule.Start(
	Character, 
	_getUnitizedDirectionOfObjectToHit(CharacterPositionV3, TargetPositionV3) * CONST_POWER
) --THIS IS THE LINE I DONT UNDERSTAND, the parameters for this function are (character,direction)

1 Like

yeah I agree

I am not trying to be mean, just honest. It’s harder to code things if your code is hard to read in the first place

what you said didn’t suggest that, I’m simply agreeing that it was hard to read and some stuff could be simplified a bit so that it’s easier to read, as you’ve said the math for the Vector3s which can just be put in one vector.

Oh no worries, I’m just a bit awooootistic so I can be blunt at times, and I have a big thing about rough code… a chip on the shoulder you may say, so I have no problems going EWWWW brother EWWWWW hahah =P

  1. i watched the videos fully, but they were those type of tutorials that kind of just tell you to copy and paste stuff without explaining very much of it

  2. i printed the value you asked before damage is applied, hit it until it died, and got this in the output:


    the part that says “nan, nan, nan” shows up because the final time I hit it, the bug happened

  3. the bug is that enemies have a random chance of disappearing from the game when they are hit

1 Like

if I were you I’d do some research on other knockback systems and roblox forces so that you can refine your script and probably fix the bug because the current way that this works makes it hard for me to come up with anything else that could probably help you.

1 Like

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