I want To Make Something Run ClientSided

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

You can use a RemoteEvent to communicate between the Server and the Client, aswell as send parameters and other information to easily. You can learn more about RemoteEvents and their usage here: RemoteEvent | Documentation - Roblox Creator Hub

Use RemoteEvent to do that.

I know But I’ve Tried Doing That Multiple Times. My Hitbox Duplicates EVEN If the self.Box Exists. I cant Seem To do It clientsided. Its Really Hard.

Have you tried removing the hitbox from the server, and only keeping it on the client? This may be the cause to the duplicate hitbox.

Yes i Exactly Did that. But I dont Know How to do it Thats The Problem.

You can use :Destroy() or game.Debris:AddItem(hitbox) to delete the hitbox on the Server, but clone it on the Client to preserve it. Alternatively, you could have a remote that deletes it for everyone except you. (I’ve explained RemoteEvents on my previous post.)

Thank You Soo Much! I Will Try It Out Right Now

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