【问题标题】:ROBLOX Get name of a player that clicked a brickROBLOX 获取点击砖块的玩家的名字
【发布时间】:2019-02-21 19:58:48
【问题描述】:

我有这个脚本:

local giver = 1

function onClicked()
    game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value = game.Players.[I NEED THE PLAYER NAME HERE].leaderstats.Clicks.Value + giver
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

现在我需要以某种方式获取点击它的玩家名称并将其放在我需要的位置。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    ClickDetectorsMouseClick 事件有“点击播放器”作为参数,所以你可以这样做:

    local giver = 1
    
    function onClicked(Player)
        Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + giver
    end
    
    script.Parent.ClickDetector.MouseClick:connect(onClicked)
    

    但是,这需要将 FilteringEnabled 设置为 false(不推荐)。

    要解决这个问题,请使用代码在砖块中创建LocalScript

    script.Parent.ClickDetector.MouseClick:connect(function(Player)
        game.ReplicatedStorage:WaitForChild("BrickClick"):InvokeServer(script.Parent)
    end)
    

    并在Script 中放置ServerScriptService

    local Listener = game.ReplicatedStorage:FindFirstChild("BrickClick")
    if Listener == nil then
        Listener = Instance.new("RemoteFunction")
        Listener.Name = "BrickClick"
        Listener.Parent = game.ReplicatedStorage
    end
    
    function Listener.OnServerInvoke(Player,Brick)
        Player.leaderstats.Clicks.Value = Player.leaderstats.Clicks.Value + 1
    end
    

    我不会将您指向wiki page 进行进一步阅读,即使它包含您需要的一些内容,但它包含的信息太少。

    ClickDetector's MouseClick infothe guide about FilteringEnabledthe guide about RemoteFunctions 更好。

    【讨论】:

    • 现在我每次都收到这个错误:02:14:08.231 - ServerScriptService.local Listener = game.ReplicatedStorage:5: bad argument #3 to 'Name' (string expected, got Object) 02:14:08.232 - Script 'ServerScriptService.local Listener = game.ReplicatedStorage', Line 5 02:14:08.232 - Stack End
    • 并且脚本也将自身重命名为整个代码文本
    • @TomLenc 修复了代码中的一些错误,但我没有得到the script also renames itself to the whole code text 的部分,它不可能重命名,是吗?如果它向你抛出错误,一切怎么能正常工作? O.o
    • 将其名称更改为:local Listener = game.ReplicatedStorage:FindFirstChild("BrickClick") if Listener == nil then Lis 这是一个屏幕截图:s22.postimg.org/cco4sv41t/bandicam_91_2.pngLis'
    • 现在显示19:06:49.970 - This function is not yet enabled! Loaded gloo library. Type _G.gloo.Help() for help.
    【解决方案2】:

    试试这个!

    script.Parent.MouseClick:Connect(function(Player)
    -- Kill The Player
    -- The parameter is referring to game.Players So if you want to do a kill button use .Character
    Player.Character:BreakJoints()
    
    -- Change The Color To Red (Other details)
        script.Parent.Parent.BrickColor = BrickColor.new("Really red")
        script.Parent.MaxActivationDistance = 0
    
    -- Wait 4 Secs
    wait(5)
    
    -- Change The Color To Green
    script.Parent.Parent.BrickColor = BrickColor.new("Lime green")
    script.Parent.MaxActivationDistance = 50
    end)
    

    【讨论】:

      猜你喜欢
      • 2013-12-18
      • 2021-10-17
      • 2021-08-05
      • 2022-06-21
      • 2022-10-05
      • 2019-06-26
      • 2015-05-17
      • 2021-01-13
      • 2018-12-16
      相关资源
      最近更新 更多