So I am trying to use the function :GetTouchingParts() on a part of my sword called “Body”, which is the blade of my sword. My problem is that it’s not working, and I don’t know why.
I’m trying to print the table that this function (is it called a function by the way, if I’m wrong someone tell me, maybe I’ll look on google *Note to self) returns of the parts that the blade interacts with, now in the gif I provide you will see my Roblox character hacking into the body of an R15 Dummy–poor Dummy, but don’t care that’s not the problem! xD Anyways. You can see that the table is being printed indefinitely, that’s because in my code I’m printing it with RunService on the Heartbeat.
So why is it not printing the table with the proper parts that it touches when it intersects the limbs and body of the R15 Dummy?
-------------
--- INDEX ---
-------------
-- SERVICES
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
-- INSTANCES
local player = script.Parent.Parent.Parent
local tool = script.Parent
local toolHandle = script.Parent.Handle
local hitBox = script.Parent.HitBox
--local humanoid = player.Character:WaitForChild("Humanoid")
--local torso = player.Character.UpperTorso
----------------------
--- INITIALIZATION ---
----------------------
-- Equipping Gladius
tool.Equipped:Connect(function()
local EquipWeapon = game.Workspace.Events.EquipWeapon
EquipWeapon:FireServer(player)
end)
tool.Activated:Connect(function()
local SlashWeapon = game.Workspace.Events.SlashWeapon
SlashWeapon:FireServer(player)
end)
local function detectHits()
print("detecting..")
local hits = toolHandle.Body:GetTouchingParts()
print(hits)
for i = 1, #hits do
print(hits)
local hit = hits[i]
local hitHumanoid = hit.Parent:FindFirstChild("Humanoid")
if hitHumanoid and hitHumanoid ~= player.Character.Humanoid then
print("YO")
local shieldActivated = hitHumanoid.Parent:FindFirstChild("Equipped", true)
if shieldActivated then
print("TEST")
if shieldActivated.Value then
print("TEST2")
local hitShield = hitHumanoid.Parent.UpperTorso.CFrame.LookVector:Dot(player.Character.UpperTorso.CFrame.LookVector)
print("TEST3")
if hitShield > -1.01 and hitShield < -.29 then
local enemyDealDamage = game.ReplicatedStorage.DealDamage
enemyDealDamage:FireServer()
end
end
end
end
end
end
-- Swinging Gladius
local guy
guy = runService.Heartbeat:Connect(detectHits)
It looks good, I’m going to use it. But I wanted to ask, shouldn’t I use my own modules? Is it a common practice for developers to use open sourced modules?
Why yes of course it is, there’s no point in making your own module if someone else already made it, you save time and chances are you’d be using something better than what you would have made. It’s a win-win really, but on the flip side, don’t go relying too heavily on other people’s work to get your game going, always keep the use of these modules on a surface/general level, by that I mean modules that handle stuff you’d expect to see in multiple games, like camera shaker, raycast hitbox, datastore etc
Yeah. My concern is that ok–if I’m making a game ideally I’m the one that knows what my game is and needs, I won’t be able to do that if I cannot create the code myself which is why I think that using this module will be more for a general/technical learning purpose rather than officially using it. I want to get to the point where I do not have to use other modules because again, if I were to create this then it must be mine.
Reading through the documentation and inside of the modules themselves-- I’m stumbling a lot, there’s honestly quite a few things that I haven’t heard of like threads, multi-threading, signals, the naming convention of things is very strange but I understand them.
I have–probably not even-- a base understanding of object-oriented programming. I mean, I know what it is but I don’t have much application knowledge of it, since the only thing I have ever done application-wise was create a game mode system called “Hardpoint” ( a multiplayer mode featured in every main Call of Duty game since Call of Duty: Black Ops II with the exception of Call of Duty: Ghosts ), and I created it by loosely looking at another game that I have that uses basically the same exact code. My point in doing this when I did it two weeks ago was to better understand and learn more about object oriented programming. My goal was to create a hardpoint objective for a game that I am working on. Quite a lot of learning curves/turns/twists in the last 5 and a half months of my everyday learning journey, I still also feel like the things that I have learned up to right now are pretty loose and need more application so that I can better understand everything in general.
using GetTouchingParts for this isn’t really ideal because it only detects collidable parts and personally have came across some inaccuracies on it. you could use Touched event instead.
Thing is, once you get experience enough, you’ll actually opt to using these modules more often than not, since you’ll understand how they work, there will be no need to make them yourself, you can however; customize them as you please when you get enough experience.
And about OOP, you are already using it, OOP is basically when an object inherits properties from another object above it in hierarchy, that’s why - for example - you can call :Destroy() on every instance on the game, because all other instances inherit that function from Instance.
If you go to the API and sort instances by Class Tree, you’ll see who inherits what from who.
That’s the premis of OOP, as for making classes of your own, that requires metatables and metamethod understanding, but you can be a successful dev without needing to make your own classes, you can do everything just fine without them.
-------------
--- INDEX ---
-------------
-- SERVICES
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
-- INSTANCES
local player = script.Parent.Parent.Parent
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local tool = script.Parent
local toolHandle = script.Parent.Handle
local hitBox = script.Parent.HitBox
--Events
local EquipWeapon = workspace.Events.EquipWeapon
local SlashWeapon = workspace.Events.SlashWeapon
local DealDamage = replicatedStorage:WaitForChild("DealDamage")
--Misc
--local humanoid = player.Character:WaitForChild("Humanoid")
--local torso = player.Character.UpperTorso
----------------------
--- INITIALIZATION ---
----------------------
-- Equipping Gladius
tool.Equipped:Connect(function()
EquipWeapon:FireServer(player)
end)
tool.Activated:Connect(function()
SlashWeapon:FireServer(player)
end)
local function detectHits()
local hits = toolHandle.Body:GetTouchingParts()
for _, hit in ipairs(hits) do
local hitHumanoid = hit.Parent:FindFirstChild("Humanoid")
if hitHumanoid then
if hitHumanoid ~= humanoid then
local hitCharacter = hit.Parent
local hitTorso = hitCharacter.UpperTorso
local hitPlayer = players:GetPlayerFromCharacter(hitCharacter)
local shieldActivated = hitCharacter:FindFirstChild("Equipped", true)
if shieldActivated then
if shieldActivated.Value then
local hitShield = hitTorso.CFrame.LookVector:Dot(hitTorso.CFrame.LookVector)
if hitShield > -1.01 and hitShield < -0.29 then
DealDamage:FireServer()
end
end
end
end
end
end
end
-- Swinging Gladius
local guy
guy = runService.Heartbeat:Connect(detectHits)