Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua Roblox API:如何对运行到盒子中的玩家/角色进行去抖动_Lua_Roblox - Fatal编程技术网

Lua Roblox API:如何对运行到盒子中的玩家/角色进行去抖动

Lua Roblox API:如何对运行到盒子中的玩家/角色进行去抖动,lua,roblox,Lua,Roblox,背景: 承载最小的对象。只有一个玩家和一个漂浮在空中并锚定的部分(矩形棱镜) 我真的不明白引擎盖下发生了什么,所以很难弄清楚。如果不取消Bounce,则在触发事件时,connect()或事件处理程序(不确定)将调用回调函数;如果不取消Bounce,则在输出框上重复打印语句时,将多次调用该函数。因此,用一个存储去盎司标志的变量(布尔类型)来解决它。然后,当玩家的模型在开箱即用的模型之外时,我尝试“取消去盎司”。但是,我不知道如何正确地做到这一点 以下是我的代码尝试: local box = gam

背景: 承载最小的对象。只有一个玩家和一个漂浮在空中并锚定的部分(矩形棱镜)

我真的不明白引擎盖下发生了什么,所以很难弄清楚。如果不取消Bounce,则在触发事件时,connect()或事件处理程序(不确定)将调用回调函数;如果不取消Bounce,则在输出框上重复打印语句时,将多次调用该函数。因此,用一个存储去盎司标志的变量(布尔类型)来解决它。然后,当玩家的模型在开箱即用的模型之外时,我尝试“取消去盎司”。但是,我不知道如何正确地做到这一点

以下是我的代码尝试:

local box = game.Workspace.BoxModel;
local debounce = false;

local function onTouchedDebounced()
    if (debounce == false)
    then
        debounce = true;
        print("Hello! onTouchedDebounced() has run.");
        box.Touched:Connect(onTouchedDebounced));
    end
end

local function onTouchedUndebounced()
    if (debounce == true) 
    then
        print("Hello! onTouchedUndebounced() has run.");
        debounce = false;
    end 
end

box.Touched:Connect(onTouchedDebounced);
box.TouchEnded:Connect(onTouchedUndebounced);

你所做的事情的核心是声音:在第一个事件后开始阻塞,然后在一段时间后解除阻塞。如果这是通过按下按钮或鼠标点击,您的解决方案将工作良好。这是复杂的接触事件,因为它与任何部分,接触它的火灾,和一个球员的角色可以有多个接触点

和事件提供对已接触零件的实例的引用

如果目标是每个玩家只触发一次box事件,或者在任何人触摸它时触发一次box事件,则可以保留当前触摸box的部件的字典。当零件接触时,增加一个计数器。当一个零件停止时,您将减小它。只有在移除所有接触点后,才能移除去抖动标志

local box = game.Workspace.BoxModel
local playersTouching = {} --<string playerName, int totalParts>

local function onTouchedDebounced(otherPart)
    -- check that the thing that touched is a player
    local playerModel = otherPart.Parent
    if not playerModel:IsA("Model") then
        warn(otherPart.Name .. " isn't a child of a character. Exiting")
        return
    end

    -- check whether this player is already touching the box
    local playerName = playerModel.Name
    local total = playersTouching[playerName]
    if total and total > 0 then
        warn(playerName .. " is already touching the box")
        playersTouching[playerName] = total + 1
        return
    end

    -- handle a new player touching the box
    playersTouching[playerName] = 1

    -- Do a thing here that you only want to happen once per event...
    print(string.format("Hello! onTouchedDebounced() has ran with %s and %s", playerName, otherPart.Name))    
end

local function onTouchedUndebounced(otherPart)
    -- decrement the counter for this player
    local playerName = otherPart.Parent.Name
    if playersTouching[playerName] == nil then
        return
    end

    local newTotal = playersTouching[playerName] - 1
    playersTouching[playerName] = newTotal

    -- if the total is back down to zero, clear the debounce flag
    if newTotal == 0 then
        playersTouching[playerName] = nil
        print(string.format("Hello! onTouchedUndebounced() has ran and %s has no more touching parts", playerName))
    end
end


box.Touched:Connect(onTouchedDebounced)
box.TouchEnded:Connect(onTouchedUndebounced)
localbox=game.Workspace.BoxModel
本地播放器触摸={}--
局部函数被反弹(其他部分)
--检查触摸的对象是否是玩家
本地playerModel=otherPart.Parent
如果不是playerModel:IsA(“模型”),则
警告(otherPart.Name.“不是字符的子级。正在退出”)
返回
结束
--检查此播放机是否已触到该框
本地playerName=playerModel.Name
本地总计=玩家触摸[玩家名称]
如果total和total>0,则
警告(playerName.“已接触到该框”)
玩家触摸[玩家名称]=总计+1
返回
结束
--处理一个新的球员接触的方块
玩家触摸[玩家名称]=1
--在这里做一件事,每件事你只想发生一次。。。
打印(string.format(“Hello!OnTouchedeBunched()已与%s和%s一起运行”、playerName、otherPart.Name))
结束
未发布的局部函数(其他部分)
--减少此玩家的计数器
本地playerName=otherPart.Parent.Name
如果playerSpoting[playerName]==nil,则
返回
结束
本地新总数=播放机触摸[播放机名称]-1
PlayerSpoting[playerName]=新总数
--如果总数回到零,则清除去盎司标志
如果newTotal==0,则
播放者触摸[播放者名称]=零
打印(string.format(“Hello!onTouchedUndebounced()已运行,并且%s没有更多触动的部分”,playerName))
结束
结束
框。已触摸:连接(已反弹)
框。触摸结束:连接(未发出警告)

谢谢!我真的很欣赏你的清晰和知识!惊人的回答,谢谢!:)你好问题!在IsA(“模型”)功能上:假设这将限制玩家对象的条件,但也限制使用Roblox模型对象类构建的NPC,对吗?是的,因为从技术上讲,
。父项:IsA(“模型”)
检查不够具体,无法仅应用于玩家角色。模型只是一组对象,NPC装备的分组方式与角色装备相同。