Get username from TextBox?

Greetings, I’v been trying to improve my admin gui with a textbox where i could enter a players username and then do random activities with it. However, iv come with a problem (possibly multiple problems) trying to send data (the name of the user) from the client to the server and I cant figure out how.

Local Script

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.user.Text == "" then --this checks if the textbox is blank, if it does then this would make the event choose a random player in the server
		game.ReplicatedStorage.events.grenade:FireServer(script.Parent.Parent.user.Text, true) --this is done by puting "true" instead of "false"
	else
		game.ReplicatedStorage.events.grenade:FireServer(script.Parent.Parent.user.Text, false)
	end
end)

Server Script

game.ReplicatedStorage.events.grenade.OnServerEvent:Connect(function(name, space)
			wait(math.random(2,5))
			local Players = game:GetService("Players")
	local PlayerList = Players:GetPlayers()
	if space == true then
		local Chosen1 = math.random(1, #PlayerList)
		local Player1 = PlayerList[Chosen1]
		local Character1 = Player1.Character
		local boom = Instance.new("Explosion", workspace)
		boom.Position = Character1.HumanoidRootPart.Position
		print("chosen random")
	else
		local Playercheck = Players:FindFirstChild(name)
		if Playercheck then
			local Chosen1 = name
			local Player1 = PlayerList[Chosen1]
			local Character1 = Player1.Character
			local boom = Instance.new("Explosion", workspace)
			boom.Position = Character1.HumanoidRootPart.Position
			print("player chosen")
		else
			print("no players found, perhaps error. sending back remotefunction (will script later)")
		end
	end
		end)

It always outputs the last print, even if the TextBox is blank or when I type my username. Can anyone help?

If anyone needs clarification, ask me.

What is the exact output your getting from the script

It prints “no players found, perhaps error. sending back remotefunction (will script later)” everytime which is what I coded for it to do to spot any errors. If you want an error then I could do that.

This line is your issue, your parameter space is what you want your name parameter to be, your parameter name is the player BECAUSE the first parameter is always the player.

In short that line needs to be changed to:

game.ReplicatedStorage.events.grenade.OnServerEvent:Connect(function(player, name, space)
1 Like

Oh thanks! It works now! Forgot that the first parameter is the one who executed it.