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
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.
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.
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.