已实现Rebirth System,如何实现重生所需价格逐次翻倍?
实现重生价格逐次翻倍的方案
核心思路
以初始重生价格为基数,根据玩家当前的重生次数计算下一次重生的所需点击数,公式为:当前所需点击数 = 基础价格 × (2 ^ 当前重生次数)。比如初始价格设为300,第1次重生需300(300×2⁰),第2次需600(300×2¹),第3次需1200(300×2²),以此类推。
修改后的完整代码
-- 定义基础重生价格 local baseRebirthCost = 300 local player = game.Players.LocalPlayer local leaderstats = player.leaderstats local clicks = leaderstats.Clicks local rebirths = leaderstats.Rebirths local gems = leaderstats.Gems -- 定义更新按钮文本的函数 local function updateButtonText() local currentCost = baseRebirthCost * (2 ^ rebirths.Value) script.Parent.Text = string.format("%d REBIRTH │ %d CLICKS", rebirths.Value + 1, currentCost) end -- 初始化按钮文本 updateButtonText() script.Parent.MouseButton1Click:Connect(function() local currentCost = baseRebirthCost * (2 ^ rebirths.Value) if clicks.Value < currentCost then script.Parent.Text = "Not Enough!" wait(2) updateButtonText() else -- 执行重生操作 rebirths.Value += 1 clicks.Value = 0 gems.Value += 10 -- 更新按钮文本为下一次重生的价格 updateButtonText() end end)
关键修改点说明
- 新增
baseRebirthCost变量统一管理初始价格,方便后续调整数值 - 编写
updateButtonText函数,根据当前重生次数动态计算并更新按钮显示的价格 - 点击判断时使用计算后的
currentCost替代固定数值,实现价格逐次翻倍 - 重生完成后立即更新按钮文本,确保玩家能看到下一次的所需价格
- 修正了原代码中按钮文本显示价格(500)与判断价格(300)不一致的问题
内容的提问来源于stack exchange,提问作者Rrsng
相关产品推荐
相关产品推荐

