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

How to fix AdminGui script for warning players with inputted reason?

local Name = game.Players.Name
local Input = script.Parent.Parent.Parent.Input.Text
local Reason = script.Parent.Parent.Parent.Reason.Text
script.Parent.MouseButton1Click()
if Input == Name then
    local WarnText = Instance.new('TextLabel')
	local Punishments = Instance.new('ScreenGui')
	Punishments.Parent = game:GetService('Players').Input:WaitForChild('PlayerGui')
	WarnText.Parent = Punishments
	WarnText.Visible = false
    WarnText.BackgroundColor3 = {0,0,0}
    WarnText.BackgroundTransparency = {0.5}
	WarnText.BorderSizePixel = 0
    WarnText.Position = {0.5, 0}
    WarnText.Size = {500,50}
    WarnText.Font = Enum.Font.GothamSemibold
    WarnText.TextColor3 = {255,255,255}
    WarnText.Text = 'Warned for reason: '..Reason
	game:GetService('Players').Input:WaitForChild('PlayerGui').WarnText.Visible = true
	wait(5)
	game:GetService('Players').Input:WaitForChild('PlayerGui').WarnText.Visible = false
end

What I want this script to do is when a valid playername has been inputted in the TextBox named 'Input' and with a reason inputted the TextBox named 'Reason' I want it to create a new instance of a textlabel and make it visible to the warned player's PlayerGui

Though as I try and warn a player, nothing shows up

https://imgur.com/rg0BO30 - A reference for the location of objects

What's wrong with my script? Hopefully it makes sense

1 answer

✓ Accepted answer

First things first, this script needs to be a LocalScript. Any time you are manipulating UI, you should do so on a client-side (on a LocalScript.) Then, instead of having all of those script.Parent.Parent... you can simply say:

local player = game.Players.LocalPlayer
local adminGui = player.PlayerGui:WaitForChild("AdminGui")

and reference your input and other stuff from adminGui

So all this LocalScript would do is get the input, check if the input is a player, and fire a RemoteEvent to the server with the player to warn's name passed in as an argument. Then, server side, you could create the Punishments GUI and parent it to the warned players' PlayerGui

Also, I would assume that your if statement is evaluating to false. game.Players.Name is just the name of the Players object, so Name would always be a string with the value "Players". If you want to see if the input is a valid player name, then you would need to loop through game.Players checking if input matches a player name. You could create another function like this to do so:

local function PlayerExists (playerName)
	-- Loop through all players
	for _, player in pairs(game.Players:GetChildren()) do
		-- Check if this current player's name is equal to the name passed in
        	if player.Name == playerName then
            		return true
        	end
    	end
	-- If no names on the player list matched, then return false.
	return false
end

So you could then change your if statement to:

if PlayerExists(Input) then
	--...
end

Hope this helped! Let me know if you have any questions!

5 comments

1

I'm confused on where I should be writing these lines and change my script, could you elborate a bit more?

0

All of the code I have provided would go in your local script. The function PlayerExists would need to be before your "if Input == Name" statement. Then, you would change that if statement to "if PlayerExists(Input) then" to check if the player exists. As for the server script you create you need to look into remote events. https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

0

If you still don't understand something let me know

1

Ah right, thanks for the elaboration.

0

Sure! If this fixed your problem, please mark my answer as the solution. :)

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