Menu
CoddyTech

مرة واحدة في كل تلويحة

الدرس 4 من 25 في دورة لعبة Roblox: ساحة القتال على Coddy.

طبعت ضربة واحدة ثلاثة أسطر، وأنقصت 30 من الدمية. جذرها وساقاها ورأسها ثلاثة أجزاء، وكلها داخل الصندوق، وقد أصاب كل جزء منها كائن Humanoid نفسه.

يعالج الجدول هذه المشكلة: تذكّر كل كائن Humanoid أصابته هذه الضربة، وتجاوز أي كائن سبق أن رأيته. يمكن أن تكون مفاتيح الجدول كائنات مثيلة:

local hit = {}
if humanoid and not hit[humanoid] then
	hit[humanoid] = true
	-- ألحِق به الضرر مرة واحدة
end

يُنشأ الجدول داخل swing، لذا تبدأ كل ضربة بجدول فارغ، ويُصاب كل واحد من عدوين داخل الصندوق مرة واحدة.

challenge icon

التحدي

سهل

اضرب كل عدو مرة واحدة في كل ضربة.

  1. في swing، أضف local hit = {} قبل الحلقة.
  2. ألحق الضرر بكائن Humanoid فقط عند تحقق if humanoid and not hit[humanoid]، واضبط hit[humanoid] = true عند فعل ذلك.

اضغط «العب» واضرب الدمية: سيعرض Output عبارة Dummy is at 90 مرة واحدة. ثم اضغط «تحقق».

جرّب بنفسك

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local lastSwing = {} -- player -> وقت آخر ضربة سيف نفّذها

-- حركات السيف الخاصة بـ Roblox
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

print("Swords deal " .. Settings.SwordDamage .. " damage")

-- ضربة سيف: تُشغَّل الحركة، ثم يُصاب كل من داخل الصندوق أمامك
local function swing(player)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = time()
	if lastSwing[player] and now - lastSwing[player] < Settings.SwingCooldown then
		return
	end
	lastSwing[player] = now
	character.Humanoid.Animator:LoadAnimation(slash):Play()
	-- الصندوق أمامك، ولا يشمل جسدك أبدًا
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -3)
	for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		-- الدرس 4: يُضاف هنا تنفيذ الضربة مرة واحدة لكل تأرجح
		if humanoid then
			humanoid:TakeDamage(Settings.SwordDamage)
			print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
		end
	end
end

-- يحصل كل لاعب على سيف في كل مرة تعود فيها شخصيته إلى الحياة
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local sword = ServerStorage.Sword:Clone()
		sword.Parent = player.Backpack
		sword.Activated:Connect(function()
			swing(player)
		end)
	end)
end)

جميع دروس لعبة Roblox: ساحة القتال

تدرّب بنفسك: Playground لـ Roblox