要将压力每上涨20点提示一次,可以在代码中添加一个变量来记录上次提示的压力值,并在每次压力更新时进行判断。下面是修改后的代码,已添加注释并翻译为简体中文:
RegisterNetEvent('hud:server:GainStress', function(amount)
local src = source
local identifier = GetIdentifier(src)
local newStress
-- 检查玩家是否在白名单中
if IsWhitelisted(src) then
return
end
-- 如果stressData未初始化,则初始化为0
if stressData[identifier] == nil then
stressData[identifier] = 0
end
-- 计算新的压力值
newStress = tonumber(stressData[identifier]) + amount
-- 限制压力值不能超过100
if newStress > 100 then
newStress = 100
end
stressData[identifier] = newStress
-- 更新数据库(如果需要)
-- ExecuteSql("UPDATE `"..Config.StressMysqlTable.."` SET stress = '"..newStress.."' WHERE identifier = '"..identifier.."'")
-- 更新客户端的压力界面
TriggerClientEvent('hud:client:UpdateStress', src, newStress)
-- 每当压力值达到30、50、70、90时给玩家发送提示
if newStress > 30 and (newStress % 20 == 0) then
Config.Notification(Config.Notifications["GETTING_STRESSED"].message, Config.Notifications["GETTING_STRESSED"].type, true, src)
end
end)
### 修改说明:
1. 添加了一条条件 `newStress % 20 == 0`,用于判断当前压力值是否是20的倍数,以便每20点变化时发送提示。
2. 代码中的变量和函数名称保持不变,以避免造成混淆。
请确保已在适当的位置初始化和管理 `stressData` 字典。该代码应放在 `server.lua` 中,因为它涉及到服务器事件的处理和数据库操作。 |