Menu
CoddyTech

انقر للتحطيم

الدرس 6 من 20 في دورة لعبة Roblox: محاكي الحيوانات الأليفة على Coddy.

يصلح ClickDetector لزر، لكن معظم ألعاب المحاكاة ترصد النقرات على العميل، لكي تستجيب اللعبة لحظة نقرك (بصوت أو اهتزاز) قبل أن يرد الخادم. تعرف فأرة العميل ما الذي نقرت عليه:

local mouse = me:GetMouse()
mouse.Button1Down:Connect(function()
	print(mouse.Target) -- الجزء الموجود تحت موضع النقر، أو nil
end)

لا يستطيع العميل تكسير كومة بنفسه: فالخادم يملك الأكوام والعملات. ويرسل طلبًا عبر RemoteEvent، وهو كائن مُخصّص لتبادل الرسائل بين العميل والخادم:

-- العميل
remotes.Hit:FireServer(pile)
-- الخادم: يضيف Roblox اللاعب الذي أرسله أولًا
remotes.Hit.OnServerEvent:Connect(function(player, pile)
end)

يصل الحدث البعيد بعد لحظة، كما يحدث عبر الشبكة. وعلى الهاتف، تُعدّ النقرة باللمس مماثلة للنقر بالفأرة. يُحذف ClickDetector والسطر local hitPile من الدرس 3: فالحدث البعيد يحلّ محلّهما.

challenge icon

التحدي

متوسط

أرسل النقرات إلى الخادم عبر RemoteEvent.

  1. في PetClient: local remotes = game.ReplicatedStorage:WaitForChild("Remotes")؛ local mouse = me:GetMouse()؛ عند mouse.Button1Down، أرسل remotes.Hit مع mouse.Target إذا كان اسمه CoinPile.
  2. في PetServer: local remotes = game.ReplicatedStorage.Remotes؛ يستدعي remotes.Hit.OnServerEvent الدالة hitPile(player, pile).
  3. احذف أسطر ClickDetector وlocal hitPile؛ واكتب local function hitPile.

اضغط «العب» وانقر على كومة: ستتكسر كما في السابق. ثم اضغط «تحقق».

جرّب بنفسك

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

local piles = workspace.Piles
local pileSpot = {} -- كل كومة -> الموضع الذي تقف عليه

print("A Meadow pile gives " .. Settings.PileValue .. " coins")

-- يحصل كل لاعب على Coins في لوحة المتصدرين
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
end)

-- يتطلب النقر على كومة الدالة hitPile، المكتوبة أسفل spawnPile: عرّفها
-- هنا، قبل أيٍّ منهما
local hitPile

-- كومة عملات في موضع: نسخة من ServerStorage.CoinPile مع صحتها
-- وقيمتها بوصفهما سمتين
local function spawnPile(spot)
	local pile = ServerStorage.CoinPile:Clone()
	pile.Position = spot.Position
	pile:SetAttribute("MaxHealth", Settings.PileHealth)
	pile:SetAttribute("Health", Settings.PileHealth)
	pile:SetAttribute("Value", Settings.PileValue)
	-- انقر عليها لضربها
	local detector = Instance.new("ClickDetector")
	detector.Parent = pile
	pileSpot[pile] = spot
	pile.Parent = piles
	detector.MouseClick:Connect(function(player)
		hitPile(player, pile)
	end)
	return pile
end

-- تُنقص الضربة 1 من صحة الكومة؛
-- عند 0 تتحطم الكومة وتمنح قيمتها
function hitPile(player, pile)
	local health = pile:GetAttribute("Health") - 1
	if health > 0 then
		pile:SetAttribute("Health", health)
		return
	end
	player.leaderstats.Coins.Value += pile:GetAttribute("Value")
	local spot = pileSpot[pile]
	pileSpot[pile] = nil
	pile:Destroy()
	-- كومة جديدة في الموضع نفسه بعد بضع ثوانٍ
	task.delay(Settings.RespawnTime, spawnPile, spot)
end

-- كومة في كل موضع في Meadow
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
	spawnPile(spot)
end

جميع دروس لعبة Roblox: محاكي الحيوانات الأليفة

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