Hey, guys. The following script is suppose to clone a Gui inside a tool and copy it to the Player's PlayerGui whenever the tool is activated (clicked once after the tool is equipped). It works perfectly in Roblox Studio, but not in the real game. If anyone explain to me what is wrong, I would gladly appreciate it.
Tool = script.Parent Player = Tool.Parent.Parent Character = Player.Character mouse = Player:GetMouse() Gui = Tool.AmmoGui Tool.Equipped:connect(function(mouse) findGui = Player.PlayerGui:FindFirstChild("AmmoGui") if findGui then findGui.Frame.Visible = true else newGui = Gui:Clone() newGui.Parent = Player.PlayerGui end end) Tool.Unequipped:connect(function(mouse) findGui = Player.PlayerGui:FindFirstChild("AmmoGui") if findGui then findGui.Frame.Visible = false else print("AmmoGui wasn't found") end end)
It's likely that you have to use a local script, because it will be a descendant of a specific player. This will also allow you to use things like game.Players.LocalPlayer
.
I would also recommend that you just change if the GUI is visible or not, instead of creating a new one.
WaitForChild() would probably be a good idea as well, in case the player just spawned and the GUI takes a minute to load.
Combine these things, and you can do something like this:
Tool.Equipped:connect(function(mouse) game.Players.LocalPlayer:WaitForChild("AmmoGui").Visible = true end)
To help you with these issues in the future: to test a place in a real environment, press F7 to start a server, and press Alt+F7 to play a player in said server. You can then look at any new errors that generate from your Output tab.