I have been trying to find a way to define a textbutton inside of a GUI inside of a regular script inside a tool. But I have not been sure how or if there is any way.
well, it’s pretty situational. first, you need to access the player, then you need to access their PlayerGui, to get access to all of their Gui. Getting the player can be done in a variety of ways such as RemoteEvent/RemoteFunctions, events, etc. But, I recommend using the Ancestry Changed event to detect when the tool gets equipped, because when the player equips a tool, it goes into their character. The event takes 2 parameters. The object that had it’s parent changed, and the object that it was parented too. use an if statement to check if the what the object was parented to has a humanoid, so we can verify that it’s a player. Then, use :GetPlayerFromCharacter() to get the player instance,. from there, you just need to access the PlayerGui, The ScreenGui, and the label.
Sorry if this is a bit confusing. It’s hard to understand via text, but I hope it helps. Also, I recommend you NOT access the gui from the server at all. It’s just bad practice. Use remotes, or something like that to change the gui using a local script.
Can you show me an example? I just want to test the limits and see what I can do.
Here you go. Although, I still recommend you dont use a server script for GUI, you should use a LocalScript.
local tool = script.Parent -- the tool
tool.AncestryChanged:Connect(function(child, parent)
local findHumanoid = parent:FindFirstChild("Humanoid")
if findHumanoid then
local player = game.Players:GetPlayerFromCharacter(findHumanoid.Parent)
local playerGUI = player.PlayerGui
local screenGUI = playerGUI.ScreenGui -- The ScreenGui object
local label = screenGUI.TextLabel -- The TextLabel object
label.Text = "Example"
end
end)
Okay so my goal is to make it so that when a tool is equipped, a gui pops up but this does not seem to be working:
local gear = script.Parent
local attackAnim = script.attack
local tool = script.Parent
local cooldown = false
gear.Activated:Connect(function()
local tool = script.Parent -- the tool
tool.AncestryChanged:Connect(function(child, parent)
local findHumanoid = parent:FindFirstChild("Humanoid")
if findHumanoid then
local player = game.Players:GetPlayerFromCharacter(findHumanoid.Parent)
local playerGUI = player.PlayerGui
local screenGUI = playerGUI.OpenLightsaber -- The ScreenGui object
local label = screenGUI.Frame -- The TextLabel object
label.Visible = true
end
end)
local humanoid = gear.Parent:FindFirstChildWhichIsA('Humanoid')
if cooldown == false then
cooldown = true
local loadedAnim = humanoid:LoadAnimation(attackAnim)
loadedAnim:Play()
local canDamage = script.Parent.canDamage
canDamage.Value = true
wait(1)
loadedAnim:Stop()
canDamage.Value = false
cooldown = false
end
end)