英文:
why isnt the gui opening when i touch the part?
问题
I have tried almost everything I can to fix it, but I'm not getting any error message, so I don't know what to do. I was wondering if one of you can help me solve this issue. Here is the code:
local frame = game.StarterGui.ShopGui.AURA_SHOP_NAME
local frame2 = game.StarterGui.ShopGui.MAIN_FRAME
local part = game.Workspace["Aura shop"].Shop.ShopPart
part.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
frame.Visible = true
frame2.Visible = true
end
end)
part.TouchEnded:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
frame.Visible = false
frame2.Visible = false
end
end)
英文:
I have tried almost everything i can to fix it but im not gettig no error message so i dont know what to do i was wondering if one of you can help me solve this issue here is the code:
local frame = game.StarterGui.ShopGui.AURA_SHOP_NAME
local frame2 = game.StarterGui.ShopGui.MAIN_FRAME
local part = game.Workspace["Aura shop"].Shop.ShopPart
part.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
frame.Visible = true
frame2.Visible = true
end
end)
part.TouchEnded:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
frame.Visible = false
frame2.Visible = false
end
end)
答案1
得分: 0
假设这是一个脚本,你有三个问题。
- 脚本在服务器上执行,所以它们无法访问玩家的 UI 元素,这些元素仅在客户端可见(有一些例外)。因此,你需要将其转换为 LocalScript 或使用 RemoteEvent 在服务器和客户端之间进行通信。
- 你正在修改 StarterGui 中的 UI,这些元素与玩家可见的不同,这些元素在其角色生成时被复制到 PlayerGui 中。
- 你的角色模型上的许多部分都可以触发 Touched 和 TouchEnded 信号,因此你需要进行一些简单的计算,以仅在所有部分停止触摸时隐藏 UI。
为解决这些问题,向 ReplicatedStorage 添加 RemoteEvent,并尝试以下代码:
-- 找到远程事件
local remoteEvent = game.ReplicatedStorage.RemoteEvent
-- 保持一个字典以计算有多少部分正在触摸商店
local counts = {} -- [玩家名字] : 计数器
-- 找到商店部分
local part = game.Workspace["Aura shop"].Shop.ShopPart
part.Touched:Connect(function(hit)
-- 当玩家触摸商店时,计数器加一
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
if not counts[player.Name] then
counts[player.Name] = 0
-- 玩家刚刚开始触摸商店,显示屏幕
remoteEvent:FireClient(player, true)
end
counts[player.Name] += 1
end
end)
part.TouchEnded:Connect(function(hit)
-- 当玩家停止触摸时,计数器减一
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
counts[player.Name] -= 1
if counts[player.Name] == 0 then
-- 所有部分都已经停止触摸商店,隐藏它
remoteEvent:FireClient(player, false)
counts[player.Name] = nil
end
end
end)
然后在 StarterGui 中放置一个 LocalScript,它将监听服务器告诉客户端显示商店的消息。
-- 找到 UI 元素
local shopGui = script.Parent.ShopGui
local frame = shopGui.AURA_SHOP_NAME
local frame2 = shopGui.MAIN_FRAME
-- 找到远程事件
local remoteEvent = game.ReplicatedStorage.RemoteEvent
-- 监听来自服务器的消息
remoteEvent.OnClientEvent:Connect(function(isVisible)
frame1.Visible = isVisible
frame2.Visible = isVisible
end)
英文:
Assuming this is a Script, you have three issues.
- Scripts execute on the server, so they don't have access to a player's UI elements, which are only visible on the client. (With some exceptions). So you'll need to convert this to a LocalScript or use a RemoteEvent to communicate between the server and client.
- You are modifying the UI in the StarterGui, which are not the same elements that are visible to a player. Those elements are copied into the PlayerGui when their character spawns.
- Many parts on your character model can fire the Touched and TouchEnded signals, so you need to do some simple accounting to only hide the UI once all of the parts stop touching.
To fix these issues, add a RemoteEvent to ReplicatedStorage, and try this :
-- find the remote event
local remoteEvent = game.ReplicatedStorage.RemoteEvent
-- keep a dictionary to count how many parts are touching the shop
local counts = {} -- [player name] : counter
-- find the shop part
local part = game.Workspace["Aura shop"].Shop.ShopPart
part.Touched:Connect(function(hit)
-- when a player touches the shop, add one to the counter
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
if not counts[player.Name] then
counts[player.Name] = 0
-- a player has just started touching the shop, show the screen
remoteEvent:FireClient(player, true)
end
counts[player.Name] += 1
end
end)
part.TouchEnded:Connect(function(hit)
-- when the player stops touching, subtract one from the counter
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
counts[player.Name] -= 1
if counts[player.Name] == 0 then
-- all the parts have stopped touching the shop, hide it
remoteEvent:FireClient(player, false)
counts[player.Name] = nil
end
end
end)
Then put a LocalScript in the StarterGui, it will listen for when the server tells the client to show the shop.
-- find the UI elements
local shopGui = script.Parent.ShopGui
local frame = shopGui.AURA_SHOP_NAME
local frame2 = shopGui.MAIN_FRAME
-- find the remote event
local remoteEvent = game.ReplicatedStorage.RemoteEvent
-- listen for messages from the server
remoteEvent.OnClientEvent:Connect(function(isVisible)
frame1.Visible = isVisible
frame2.Visible = isVisible
end)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论