So Im Making a New hitbox Module That Works Just Fine. But I want To make A Function Run Client Sided. I Tried Multiple Ways But It Doesnt Work. Here Is My Script:
local module = {}
module.__index = module
function module.new()
return setmetatable({
Size = Vector3.new(6,6,6),
CFrame = CFrame.new(0,0,0),
Offset = CFrame.new(0,0,0),
DetectionMode = "Normal",
Visualizer = true,
Box = nil,
newBox = nil,
Touched = GoodSignal.new(),
Hitlist = {},
Connection = nil,
OverlapParams = OverlapParams.new()
}, module)
end
function module:Visualize()
local cframetype = typeof(self.CFrame)
local pointcframe
if cframetype == "CFrame" then
pointcframe = self.CFrame
elseif cframetype == "Instance" then
pointcframe = self.CFrame.CFrame
end
if self.newBox then
self.newBox.CFrame = pointcframe * self.Offset
end
if self.Box then
self.Box.CFrame = pointcframe * self.Offset
local dist = (self.OldCFrame.p - (pointcframe * self.Offset).p ).Magnitude
warn(dist)
if dist > .2 and self.OldCFrame ~= self.Box.CFrame then
self.newBox = Instance.new("Part", workspace.Terrain)
self.newBox.Name = "Visualizer"
self.newBox.Size = self.Size
self.newBox.Material = Enum.Material.ForceField
self.newBox.CFrame = pointcframe * self.Offset
self.newBox.Color = Color3.fromRGB(255,0,0)
self.newBox.CanCollide = false
self.newBox.Anchored = true
self.newBox.Massless = true
if not self.Visualizer then
self.newBox.Transparency = 1
else
self.newBox.Transparency = .3
end
game.Debris:AddItem(self.newBox, .2)
end
else
self.Box = Instance.new("Part", workspace.Terrain)
self.Box.Name = "Visualizer"
self.Box.Size = self.Size
self.Box.Material = Enum.Material.ForceField
self.Box.CFrame = pointcframe * self.Offset
self.Box.Color = Color3.fromRGB(255,0,0)
self.Box.CanCollide = false
self.Box.Anchored = true
self.Box.Massless = true
self.Box.Locked = true
local Hightlight = Instance.new("Highlight", self.Box)
Hightlight.Name = "Hightlight"
Hightlight.FillTransparency = 1
Hightlight.OutlineColor = Color3.fromRGB(223, 0, 0)
Hightlight.OutlineTransparency = 0
Hightlight.Adornee = self.Box
if not self.Visualizer then
self.Box.Transparency = 1
else
self.Box.Transparency = .1
end
end
self.OldCFrame = self.Box.CFrame
end
function module:Cast()
local cframetype = typeof(self.CFrame)
local pointcframe
if cframetype == "CFrame" then
pointcframe = self.CFrame
elseif cframetype == "Instance" then
pointcframe = self.CFrame.CFrame
end
local partsinbounds = workspace:GetPartBoundsInBox(pointcframe * self.Offset, self.Size, self.OverlapParams)
for i, hit in pairs(partsinbounds) do
local Enemy = hit:FindFirstAncestorOfClass("Model") or hit.Parent
local Humanoid = Enemy:FindFirstChildOfClass("Humanoid")
if self.DetectionMode == "Normal" and Enemy and Humanoid and not table.find(self.Hitlist, Enemy) then
table.insert(self.Hitlist, Enemy)
self.Touched:Fire(Enemy, Humanoid)
elseif self.DetectionMode == "HitParts" then
self.Touched:Fire(Enemy)
end
end
end
function module:Start()
task.spawn(function()
self.Connection = game:GetService("RunService").Heartbeat:Connect(function()
self:Visualize()
self:Cast()
end)
end)
end
function module:Stop()
self.Connection:Disconnect()
self.Touched:DisconnectAll()
self.Hitlist = {}
if self.Box then
self.Box:Destroy()
end
if self.newBox then
self.newBox:Destroy()
end
end
return module