User interface 使用rbx.lua,如何编辑GUI属性?

User interface 使用rbx.lua,如何编辑GUI属性?,user-interface,lua,roblox,User Interface,Lua,Roblox,将UI_ELEMENT.Visible更改为true,然后更改为false将显示并隐藏UI元素,但是当我再次将其切换为true时,它不会重新出现。我相信这可能是我如何做的问题,而不是我在做什么 嗨, 我是Roblox Lua的新手(但我有Javascript和C#经验)。我正在制作一个“车库”或“零件”GUI。我正在尝试使文本对象上的点击检测器将UI元素的UI_ELEMENT.Visible设置为true。还有一个文本按钮(前面提到的UI元素的一部分)将UI_element.Visible设置回

将UI_ELEMENT.Visible更改为true,然后更改为false将显示并隐藏UI元素,但是当我再次将其切换为true时,它不会重新出现。我相信这可能是我如何做的问题,而不是我在做什么

嗨, 我是Roblox Lua的新手(但我有Javascript和C#经验)。我正在制作一个“车库”或“零件”GUI。我正在尝试使文本对象上的点击检测器将UI元素的UI_ELEMENT.Visible设置为true。还有一个文本按钮(前面提到的UI元素的一部分)将UI_element.Visible设置回false

这个过程运行良好,直到我多次运行它(例如设置为true,然后设置为false,然后再次设置为true)。UI_ELEMENT.Visible被“锁定”为true(因为将其设置为false只会导致在下一帧将其设置为true),但UI不会显示

代码:

click_detector1.MouseClick:connect(function(player) -- When clicked

  
    _G.PlayerInfo[player.Name].status = "In Garage" -- set player status to in garage (works fine no issues)
    
    _G.PlayerInfo[player.Name].topbar = "" -- reset topbar (works)

    
    print("this is only supposed to happen once") -- a check to see if this is running more than once

    
    game.Players[tostring(player.Name)].PlayerGui.Garage.menu.Visible = true -- one way that should work

    --.Enabled = true -- another way that should work

    --.menu.Position = UDim2.new(0.5, 0, 0,0) -- another way that should work (setting position to center of screen)

end)
上面是一个服务器脚本(我们称之为脚本#1)

上面是一个本地脚本(我们将此脚本称为#2)

有趣的是,我在“应该工作的另一种方式”中提出的方法实际上没有一种比循环的初始第一个周期更有效(例如设置为true,然后设置为false,然后再设置为true)

还要记录测试以查看它们是否运行多次,每次循环测试时只运行一次(就像应该的那样)。但是,这意味着将其设置为可见的代码也在运行,但不会记录错误或执行它应该执行的操作


谢谢,Daniel Morgan

我认为问题在于您使用服务器脚本和本地脚本。本地脚本只会更改播放器客户端上的内容。非本地脚本更改服务器上的内容。换句话说,非本地脚本影响每个人,而本地脚本只影响单个玩家

通过本地脚本将GUI的可见性设置为false只会更改播放器客户端上的GUI。但是,服务器仍然会看到播放器的GUI可见性为true。这种差异可能会给您带来问题

我建议使用RemoteEvents的另一种方法。与在脚本#2中将本地脚本中的GUI可见性更改为false不同,我将使用RemoteEvent来实现这一点。在您的情况下,它看起来如下所示:

以下是非本地脚本中的内容:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
 
local remoteEvent = Instance.new("RemoteEvent",ReplicatedStorage)
remoteEvent.Name = "MyRemoteEventName"
 
-- player is the player object, visibility is a boolean (true or false)
local function ChangeGuiVisibility(player, visibility)
    player.PlayerGui.Garage.menu.Visible = visibility
end
 
-- Call "onCreatePart()" when the client fires the remote event
remoteEvent.OnServerEvent:Connect(onCreatePart)
您可以从本地脚本触发此远程事件,如下所示:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local visiblity = false
local remoteEvent = ReplicatedStorage:WaitForChild("MyRemoteEventName")
 
-- Fire the remote event. 
remoteEvent:FireServer(visibility)-- NOTE: the player object is automatically passed in as the first argument to FireServer()
以下是有关RemoteEvents的非常好的指南:

基本上,RemoteEvents允许您弥合客户机和服务器之间的差距。客户端可以触发服务器将响应的事件。不久前,罗布罗克斯的情况并非如此。客户端的更改对服务器可见,这使得利用游戏变得非常容易

另外,我想建议对您的第一个脚本进行一些更改,这可能对您将来有所帮助。 而不是:

game.Players[tostring(player.Name)].PlayerGui.Garage.menu.Visible = true
试一试

无需访问游戏。玩家,因为MouseClick事件已返回单击按钮的玩家对象。我使用FindFirstChild检查车库GUI是否存在。这可以防止潜在错误的发生,通常是一种良好的做法


希望这会有所帮助,如果您仍然有问题,请继续跟进。

如果我正确理解您的问题,您是否会问为什么在打开/关闭几次后,此代码开始失败?@Kylaaa是的,这是我不理解的。
game.Players[tostring(player.Name)].PlayerGui.Garage.menu.Visible = true
 if player.PlayerGui:FindFirstChild("Garage") then
    player.PlayerGui.Garage.menu.Visible = true
 end