LocalScript in to ServerSide

Hey, all! :wave:

I’m hoping someone has the adequate knowledge to convert the LocalScript provided below in to a serverside script most likely using a RemoteEvent, as I’m unsure of how to do so myself. To describe what the script does - it’s for a hosting gui at my training center. It changes the overhead rank name of the inputted player’s username. Let me know if you can assist with converting this script to serverside; thank you in advance! :grinning_face_with_smiling_eyes:

local rankButton = script.Parent.Parent.TextButton

rankButton.MouseButton1Down:Connect(function()
	local characters = string.lower(script.Parent.Text)
	for i, player in pairs(game.Players:GetChildren()) do
        local plrName = string.lower(player.Name)
		if string.sub(plrName,1,#characters) == characters then
			player.Character.Head.Rank.Frame.Name1.Text = "test"
		end
	end
end)

So I believe you are saying you want to use a RemoteEvent from the client to the server, correct?

If you’d like to change something from the client to all players, here is a method using a RemoteEvent.

You can create a RemoteEvent inside of ReplicatedStorage and call it something, for example “TextEvent”.

The server script can be what is shown bellow:

local Event = game.ReplicatedStorage:WaitForChild("TextEvent") -- Define the event

Event.OnServerEvent:Connect(function(plr, text)
	local Characters = string.lower(text)
	
	for i, player in pairs(game.Players:GetPlayers()) do
		local PlrName = string.lower(plr.Name)

		if string.sub(PlrName, 1, #Characters) == Characters then
			plr.Character.Head.Rank.Frame.Name1.Text = "Test" -- Change to the text variable if you'd like to pass custom text
		end
	end
end)

And the local script:

local TextEvent = game.ReplicatedStorage:WaitForChild("TextEvent")
local RankButton = script.Parent

RankButton.Activated:Connect(function()
    TextEvent:FireServer("Text") -- You can change the text with whatever you'd like
end)

Although I’m giving you code now, next time I suggest finding tutorials/searching the dev forum for an answer.

1 Like

Yes, correct. Apologies, I didn’t see this!

Thank you so much, I’ll try this out. Sorry, I’ll look around before posting next time! :heart:

1 Like

All good, good luck with whatever you’re making! :+1:

1 Like

Unfortunately it’s not working, I’m unsure as to why. :frowning:

Edit: It’s now working, thank you so much!

Also, how would I make an ImageLabel on a GUI change to the player’s head image when the button is pressed?

You can use GetUserThumbnailAsync to get the thumbnail of the player and change it with that. the page with the text highlighted in blue should give you a good explanation of how it should be done and also provides you with an example

1 Like

Ah, thank you so much! :heart:

1 Like

Anytime! If you have anymore issues with your scripts you can reply with your issue, or alternatively if you have a separate issue, you can make another post!

1 Like

I’m experiencing an issue. It’s executing the script on the player who clicks the button, not the player who’s username is inputted.

https://drive.google.com/file/d/1qUMx0ukcrzgZG5lhZXkgjeRNrzYRgOOg/view

Could you provide me the code you currently have as of now

Sure! Here’s a link.

https://drive.google.com/file/d/1axM0VraaZahKahqGCyxHcp74y8zT7P5_/view

Well the first thing I notice is that in the FireServer you gave it “” as a parameter, which means nothing. The issue with that is that means it’ll set any player on that role regardless of what you type. Did you mean to set the parameter as the Textbox’s text?

Also if you want to reliably test this, you’ll have to make a local server and test it from there since you’re only player there

The thing is, when I put some text in the parameter it doesn’t work. What script would I put in to set the parameter as the TextBox’s text? Also, I did test it on a local server in studio with 2 players!

Also, just to confirm, the Text in the TextBox will be the first few letters of the player who the script is supposed to execute on (rank).

Basically the paramter to get the textbox’s text would basically just be giving it the location of the textbox and giving it the text

Example, say your textbox is in the Parent of the button and it’s called Roles, you would do

TextEvent:FireServer(script.Parent.Parent.Roles.Text)

But in your case, replace script.Parent.Parent.Roles with the location of your textbox making sure you keep .Text to get the text

This seems to have worked, thank you so much! The only issue now is when there is NO text in the TextBox, it’ll execute the script on the player who clicked the button. (It works as expected when there is a username in the TextBox, though.) How could I make it so it doesn’t do anything when there is no text in the TextBox?

Could I do something like:

if (textboxlocationhere.text) = “” then

end

?

I think that would work, but I think it has to be

if (textboxlocationhere.Text) ~= "" then

end

That will only make it execute the code inside if the text is not nothing/empty. Also, .Text must have a capital t cause case sensitivity. The code you provided now that I see w ouldn’t work eitherways since = is used as an assignment operator