当触摸砖块时,如何让脚本调出商店 GUI?
我应该如何在商店 GUI 中制作“购买”的东西?
您需要自己制作商店界面,但我会给您“GUI Giver”脚本。
注意:您必须将脚本放在Brick /Part 中。
local GUI = game:GetService("ServerStorage"):WaitForChild("GUI") -- Recommended to place your GUI inside of ServerStorage
script.Parent.Touched:Connect(function(hit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if Player then
if not Player:WaitForChild("PlayerGui"):FindFirstChild(GUI.Name) then
GUI:Clone().Parent = Player.PlayerGui
end
end
end)
Make a script that connects a brick's 'Touched' event to a function which uses the "getPlayerFromCharacter" method of game.Players to find the player then inserts the GUI into the player's "PlayerGui". For example:
function newGUI()
--enter something that makes a shop GUI then at the end returns the 'ScreenGui' it's in.
end
script.Parent.Touched:connect(function(hit)
local player = game.Players:getPlayerFromCharacter(hit.Parent);
if player ~= nil then
newGUI().Parent = player.PlayerGui;
end
end)
以下代码可用于为玩家提供商店 gui:
local ShopGui = game.Lighting.ShopGui -- This should be the location of your gui
local ShopPart = workspace.ShopPart -- This should be the shop part
ShopPart.Touched:connect(function(hit)
if hit.Parent == nil then return end
if hit.Name ~= "Torso" then return end
local Player = game.Players:playerFromCharacter(hit.Parent)
if Player == nil then return end
if _G[Player] == nil then _G[Player] = {} end
if _G[Player].ShopGui == nil then
_G[Player].ShopGui = ShopGui:Clone()
_G[Player].ShopGui.Parent = Player.PlayerGui
end
end)
ShopPart.TouchEnded:connect(function(hit)
if hit.Parent == nil then return end
local Player = game.Players:playerFromCharacter(hit.Parent)
if Player == nil then return end
if _G[Player] == nil then return end
if _G[Player].ShopGui ~= nil then
_G[Player].ShopGui:Destroy()
_G[Player].ShopGui = nil
end
end)
注意“ShopPart”应该是覆盖整个店铺区域的很大一部分(最好是不可见的)
然后你还必须建立一个商店 gui。
在商店 gui 中,您应该制作每个包含以下脚本的 TextButtons(或图像按钮):
local Cost = 100
local ThingToBuy = game.Lighting.Weapon -- Make sure this is right
script.Parent.MouseButton1Down:connect(function()
local Player = script.Parent.Parent.Parent.Parent -- Make sure this is correct
if Player.leaderstats["money"].Value >= Cost then -- Change "money" to anything you want (it must be in the leaderstats tho)
Player.leaderstats["money"].Value = Player.leaderstats["money"].Value - Cost
ThingToBuy:Clone().Parent = Player.Backpack
-- GuiToBuy:Clone().Parent = Player.PlayerGui
end
end)
该代码未经测试,因此可能包含错误。你可能需要改变比提到的更多的东西。但它应该让您了解如何制作商店 gui =)