Hello! I am trying to make a thing where once you press E it switches the text from "Normal" to "Sword" but for some reason anything i do in a localscript relating to GUI's seem to not work.
Does anyone know why its not working?
Local Script: (In StarterPlayerScripts)
local UIS = game:GetService("UserInputService") local normalmoves = game.StarterGui.DefaultUI.Moves.NormalMoves:GetChildren() local modetext = game.StarterGui.DefaultUI.Moves.Mode local backpack = script.Parent.Parent:WaitForChild("Backpack") mode = 1 UIS.InputBegan:Connect(function(input, busy) if input.KeyCode == Enum.KeyCode.E and mode == 1 then print("Player switched to sword mode") modetext.Text = "Sword" -- This code doesnt work mode = 2 elseif input.KeyCode == Enum.KeyCode.E and mode == 12 then print("Player switched to Normal mode") modetext.Text = "Normal" -- This code doesnt work mode = 1 end end)
I don't understand why anything i do GUI's doesnt work in a localscript
StarterGui is just a container. When a player joins, StarterGui will replicate its descendants into the PlayerGui which is in the PlayerObject.
So I see the problem on lines 2 and 3.
Instead of this:
local normalmoves = game.StarterGui.DefaultUI.Moves.NormalMoves:GetChildren() local modetext = game.StarterGui.DefaultUI.Moves.Mode
Change it to this:
local playerGui = game.Players.LocalPlayer.PlayerGui local normalmoves = playerGui.DefaultUI.Moves.NormalMoves:GetChildren() local modetext = playerGui.DefaultUI.Moves.Mode
You might have to add a wait before the LocalScript starts because the LocalScript runs quicker than the player's descendants are loaded.