You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Roblox leaderstats脚本实现触碰部件获点数而非每秒加1

Roblox Leaderstats 触碰部件加点数修复方案

核心问题分析

你当前的脚本缺少部件触碰检测逻辑,且保留了每秒自动加点数的循环。要实现触碰pointPart1加10点的功能,需要移除自动加点数的循环,添加Touched事件处理,同时注意几个关键细节:

  • 确认触碰部件的是玩家角色的肢体(而非其他物体)
  • 通过角色的Humanoid关联到对应的玩家对象
  • 加入防抖机制,避免短时间内重复触发加点数

修改后的完整代码

local Players = game:GetService("Players")
local pointPart1 = game.Workspace.PointsPlaceFolder.PointPart1
local debounceTime = 1 -- 防抖间隔,单位:秒

-- 处理角色加载逻辑(原逻辑保留)
local function onCharacterAdded(character, player)
    player:SetAttribute("IsAlive", true)
    
    local humanoid = character:WaitForChild("Humanoid")
    
    humanoid.Died:Connect(function()
        local points = player.leaderstats.Points
        points.Value = 0
        player:SetAttribute("IsAlive", false)
    end)
end

-- 处理玩家加入逻辑(原逻辑保留)
local function onPlayerAdded(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    local points = Instance.new("IntValue")
    points.Name = "Points"
    points.Value = 0
    points.Parent = leaderstats
    
    player:SetAttribute("IsAlive", false)
    
    player.CharacterAdded:Connect(function(character)
        onCharacterAdded(character, player)
    end)
end

Players.PlayerAdded:Connect(onPlayerAdded)

-- 新增:部件触碰事件处理
pointPart1.Touched:Connect(function(hit)
    -- 检测触碰对象是否有Humanoid(判断是否为玩家角色)
    local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if not humanoid then return end
    
    -- 通过Humanoid获取对应的玩家
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    if not player then return end
    
    -- 检查玩家是否存活且有leaderstats
    if not player:GetAttribute("IsAlive") or not player:FindFirstChild("leaderstats") then return end
    
    -- 防抖处理:避免短时间内重复触发
    local lastTouch = player:GetAttribute("LastPointTouch") or 0
    if tick() - lastTouch < debounceTime then return end
    player:SetAttribute("LastPointTouch", tick())
    
    -- 添加点数
    local points = player.leaderstats.Points
    points.Value += 10
end)

关键改动说明

  1. 移除自动加点数循环:删除原脚本末尾的while true循环,取消每秒自动加点数的逻辑
  2. 添加Touched事件监听:给pointPart1绑定触碰事件,处理每一次有效触碰
  3. 玩家身份校验:通过hit.Parent查找Humanoid,再用GetPlayerFromCharacter获取玩家,确保只有玩家触碰才触发点数增加
  4. 防抖机制:用LastPointTouch属性记录上次触碰时间,防止玩家反复蹭部件刷点数
  5. 状态校验:确保玩家处于存活状态且存在leaderstats,避免运行时报错

内容的提问来源于stack exchange,提问作者Maximus Ekon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 09:33:24