Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Admin command not working/Can I access a player's playerGui through script?

I'm currently making custom admin commands. One of the commands I'm making is "/menu" command where when you say the command, it has a gui come onto the sender's gui. For some reason, this is not working. Help?

Heres my script:

local commands = {}

local prefix = "/"

commands.menu = function(sender)
	local name = sender.Name
	local gui = script.MenuGui:Clone()
	gui.Parent = game.Players.name.PlayerGui
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message,recipient)
		message = string.lower(message) --if someone says "Menu" or the command with any uppercase letters, it would be converted into lowercase lol
		
		local splitString = message:split(" ")
		
		local slashCommand = splitString[1]
		
		local cmd = slashCommand:split(prefix)
		
		local name = cmd[2]
		
		if commands[name] then
			commands[name]()
		end
	end)
end)

Any help would be appreciated.

4 comments

0

No, you can't acces the player GUI through a script.... but can you help me here? my roblox animation won't play through the actual game.. it only plays in Roblox Studio...

0

"Name" is equal to the player's name, not the command name. You also are using the incorrect format to use GUIs in a script.

0

You should use events when doing this so player is defined for finding playergui

0

like when firing the server put player in the brackets ()

2 answers

I don't really like the script that is there, has bad learning structure, but here's mine. I've recreated it in a way that get's the command's name by string so that you can check what the command is. You can use this so also get the arguments with the supplied table value (CommandArguments), I hope this helps.

local Commands = {}
local Prefix = ("/")

function Commands:OpenMenus(Speaker)
	local Name = Speaker.Name
	local MenuGui = script.MenuGui:Clone()
	MenuGui.Parent = Speaker:FindFirstChildOfClass("PlayerGui")
end

function BindToPlayer(Player)
	Player.Chatted:Connect(function(Message)
		if string.sub(Message,1,string.len(Prefix)) == Prefix then
			local LowerCaseMessage = string.lower(Message)
			local CommandInfo = {}
			CommandInfo.CommandName = 
				string.sub(
					LowerCaseMessage,
					string.find(LowerCaseMessage,"/") + 1,
					string.find(LowerCaseMessage," ") - 1
				)
			CommandInfo.CommandArguments = string.split()
			if CommandInfo.CommandName == "menu" then
				Commands:OpenMenus(Player)
			end
		end
	end)
end

for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
	BindToPlayer(Player)
end
game:GetService("Players").PlayerAdded:Connect(BindToPlayer)

You had the incorrect format for accessing the PlayerGui through a script. You also had incorrect syntax and variables.

Fix:

local prefix = "/"

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message,recipient)
        message = string.lower(message) --if someone says "Menu" or the command with any uppercase letters, it would be converted into lowercase lol

        local splitString = message:split(" ")

        local slashCommand = splitString[1]

        local cmd = slashCommand:split(prefix)

        if splitString[1] == prefix.."message" then
			local name = splitString[2]
			local gui = script.MenuGui:Clone()
			gui.Parent = game.Players["name"]:WaitForChild("PlayerGui")
		end
    end)
end)

2 comments

0

I noticed you were using AlvinBlox's format. While he's usually reliable, I HATE his admin commands format. I find it very confusing, as it has a lot of extra code that you don't need.

0

I remember watching his tutorial about a year ago, it kind of stuck with me. Sorry!

This discussion is preserved as it was. New answers, comments, and votes are closed.