Some questions I have regarding scripting combat

Hello, I’m looking for ways I can get better at combat.

Questions:
–Should I use FireAllClients to make hitboxes?
–Can I use runservice to make effects more optimal?

Updated version(Any room for improvements?):

Server script(no sanity checks):

local RS = game:GetService("ReplicatedStorage")

RS.Check.OnServerEvent:Connect(function(plr)
	RS.Combat:FireAllClients(plr)--Sending the plr that fired this chaos
end)

Main local script:

local RS = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local hitbox = RS.HitBox

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local debounce1 = false
local debounce = false

local hits = 0
local lasttime = 0

mouse.Button1Down:Connect(function()
	if not debounce1 then
		debounce1 = true
		RS.Check:FireServer()
		task.wait(0.5)
		debounce1 = false
	end
end)

RS.Combat.OnClientEvent:Connect(function(firee)
	local function createhitbox()
		local hittime = 0
		local cd = 2
		local char = firee.Character
		local hum = char.Humanoid
		local HRP = char.HumanoidRootPart
		local newhitbox = hitbox:Clone()
		local function emit()
			newhitbox.Punch:Emit(1)
		end
		local function create()
			newhitbox.Parent = workspace
			newhitbox.CFrame = HRP.CFrame + HRP.CFrame.LookVector * 3
		end
		local function nospeed()
			hum.WalkSpeed = 0
		end
		local function normalspeed()
			hum.WalkSpeed = 14
			hum.JumpPower = 0
			task.wait(1)
			hum.WalkSpeed = 14
			hum.JumpPower = 50
		end
		local function playsound()
			newhitbox.Sound:Play()
		end
		task.spawn(normalspeed)
		local function spawning()
			task.spawn(create)
			newhitbox.Touched:Connect(function(hit)
				if not hit:IsDescendantOf(char) and hit.Parent:FindFirstChild("Humanoid") and not debounce then
					local target = hit.Parent
					local enemyHRP = target.HumanoidRootPart
					local enemyHumanoid = target.Humanoid
					local function finalhit()
						task.spawn(emit)
						task.spawn(playsound)
						hits = 0
						target.Humanoid:TakeDamage(35)
						enemyHRP.AssemblyLinearVelocity = newhitbox.CFrame.LookVector * 100
						enemyHumanoid.WalkSpeed = 14
						enemyHumanoid.JumpPower = 50
					end
					local function normalhit()
						task.spawn(emit)
						print("Happened")
						task.spawn(playsound)
						target.Humanoid:TakeDamage(15)
						enemyHRP.AssemblyLinearVelocity = newhitbox.CFrame.LookVector * 10
						enemyHumanoid.WalkSpeed = 0
						enemyHumanoid.JumpPower = 0
						if os.time() - hittime >= cd then
							hittime = os.time()
							enemyHumanoid.WalkSpeed = 14
							enemyHumanoid.JumpPower = 50
						end
					end
					debounce = true
					hits += 1
					if hits == 3 then
						task.spawn(finalhit)
					elseif hits < 3 then
						task.spawn(normalhit)
					end
					task.wait(0.25)
					debounce = false
				end
			end)
			debris:AddItem(newhitbox,1)
		end
		task.spawn(spawning)
	end
	local function animate()
		local hum = firee.Character.Humanoid
		local anim = Instance.new("Animation")
		local animtable = {
			"http://www.roblox.com/asset/?id=15678572258",
			"http://www.roblox.com/asset/?id=15678663840"
		}

		for i,v in pairs(animtable) do
			local random = math.random(1,#animtable)
			local chosen = animtable[random]
			anim.AnimationId = chosen
		end

		local animtrack = hum:LoadAnimation(anim)
		if animtrack.IsPlaying == true then
			animtrack:Stop()
		end
		animtrack:Play()
	end
	task.spawn(animate)
	task.spawn(createhitbox)
end)

No, you shouldn’t rely on the client for that. Hitboxes should be created on the server.

Not really, but they should be on the client.

1 Like

I’ve seen people say sanity checks should be made on the server and calculations/hitboxes should be made on the client because it would be more performant. Previously when I scripted my hitbox on the server my impact particle loaded half the time I punched my testing dummies. When I tried this with fireallclients, it emitted unlike in the server.

Updated version:

Server script(no sanity checks):

local RS = game:GetService("ReplicatedStorage")

RS.Check.OnServerEvent:Connect(function(plr)
	RS.Combat:FireAllClients(plr)--Sending the plr that fired this chaos
end)

Main local script:

local RS = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
local hitbox = RS.HitBox

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local debounce1 = false
local debounce = false

local hits = 0
local lasttime = 0

mouse.Button1Down:Connect(function()
	if not debounce1 then
		debounce1 = true
		RS.Check:FireServer()
		task.wait(0.5)
		debounce1 = false
	end
end)

RS.Combat.OnClientEvent:Connect(function(firee)
	local function createhitbox()
		local hittime = 0
		local cd = 2
		local char = firee.Character
		local hum = char.Humanoid
		local HRP = char.HumanoidRootPart
		local newhitbox = hitbox:Clone()
		local function emit()
			newhitbox.Punch:Emit(1)
		end
		local function create()
			newhitbox.Parent = workspace
			newhitbox.CFrame = HRP.CFrame + HRP.CFrame.LookVector * 3
		end
		local function nospeed()
			hum.WalkSpeed = 0
		end
		local function normalspeed()
			hum.WalkSpeed = 14
			hum.JumpPower = 0
			task.wait(1)
			hum.WalkSpeed = 14
			hum.JumpPower = 50
		end
		local function playsound()
			newhitbox.Sound:Play()
		end
		task.spawn(normalspeed)
		local function spawning()
			task.spawn(create)
			newhitbox.Touched:Connect(function(hit)
				if not hit:IsDescendantOf(char) and hit.Parent:FindFirstChild("Humanoid") and not debounce then
					local target = hit.Parent
					local enemyHRP = target.HumanoidRootPart
					local enemyHumanoid = target.Humanoid
					local function finalhit()
						task.spawn(emit)
						task.spawn(playsound)
						hits = 0
						target.Humanoid:TakeDamage(35)
						enemyHRP.AssemblyLinearVelocity = newhitbox.CFrame.LookVector * 100
						enemyHumanoid.WalkSpeed = 14
						enemyHumanoid.JumpPower = 50
					end
					local function normalhit()
						task.spawn(emit)
						print("Happened")
						task.spawn(playsound)
						target.Humanoid:TakeDamage(15)
						enemyHRP.AssemblyLinearVelocity = newhitbox.CFrame.LookVector * 10
						enemyHumanoid.WalkSpeed = 0
						enemyHumanoid.JumpPower = 0
						if os.time() - hittime >= cd then
							hittime = os.time()
							enemyHumanoid.WalkSpeed = 14
							enemyHumanoid.JumpPower = 50
						end
					end
					debounce = true
					hits += 1
					if hits == 3 then
						task.spawn(finalhit)
					elseif hits < 3 then
						task.spawn(normalhit)
					end
					task.wait(0.25)
					debounce = false
				end
			end)
			debris:AddItem(newhitbox,1)
		end
		task.spawn(spawning)
	end
	local function animate()
		local hum = firee.Character.Humanoid
		local anim = Instance.new("Animation")
		local animtable = {
			"http://www.roblox.com/asset/?id=15678572258",
			"http://www.roblox.com/asset/?id=15678663840"
		}

		for i,v in pairs(animtable) do
			local random = math.random(1,#animtable)
			local chosen = animtable[random]
			anim.AnimationId = chosen
		end

		local animtrack = hum:LoadAnimation(anim)
		if animtrack.IsPlaying == true then
			animtrack:Stop()
		end
		animtrack:Play()
	end
	task.spawn(animate)
	task.spawn(createhitbox)
end)

Video:

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