How do I make a play button which respawns people when they click play

What I am looking to make
I’m trying to make a play button where if I click on it, it respawns you and auto assigns you to a certain team if you’re in a certain group.
I have no idea how to do this, please help.

Insert a Script into a button and write:

local player = script.Parent.Parent.Parent.Parent.Name --Isert one parent if you are not working in a button located just into a ScreenGui, every parent will go back of 1 object in the explorer
 
script.Parent.MouseButton1Click:Connect(function()
    game.Players:FindFirstChild(player).Team = game.Teams.Red --Change Red with the name of your team
end

It should look like this:
Cattura

Why are you using so many .Parents?

Just do:

local player = game.Players.LocalPlayer

Also that will only change it on the client. RemoteEvents are needed here. I don’t think server scripts will run in a gui button.

1 Like

No because it can be used only on a LocalScript and if you change team with a LocalScript the other players will cannot see the your actual team in the leaderboard.

Because I have to get the player starting from the button.

If you want to use

local player = game.Players.LocalPlayer 

you need a LocalScript and a RemoteEvent to fire the variable to the server and it become more complicated.

You do realize normal scripts do not function in the PlayerGui, right?
As @synical4 has stated,

So, in order to do this:

LocalScript:

script.Parent.Activated:Connect(function()
    PathToRemoteEventHere:Fire()
end)

ServerScript:

PathToRemoteEventHere.Fired:Connect(function(Player)

    if PlayerIsInGroup(GroupId) then
          Player.Team = Teams.GroupTeam
     end

    Player:LoadCharacter()
end)

Replace PathToRemoteEvent here with the path to the remote event.
Scripts have not been tested. This is meant solely as a demonstration.

1 Like

You’re wrong, I’ve used a Script to change team in my game and it works perfectly.

1 Like

It’s bad practice to put important serverside operations where a player can control it, since exploiters have full ability to manipulate scripts there.

As i know they can exploit the Client too.

Yes, but server scripts have more power over local scripts, and has the ability to bypass replication filtering. This leaves your game highly vulnerable to exploits, and should NEVER be done.

Edit:

Alright you can ignore this reply then

Uh, I didn’t know, so should I use a LocalScript that send to the server the name of the player that click the button and use a Script to change the team?

script.Parent:FindFirstAncestorOfClass("Player")

You don’t need to send any other information with Remote Events, since the first parameter is always set to the player who sends it.

Also, exploiters can manipulate parameters sent via remote events.

1 Like

They do run, but it’s not recommended

1 Like

Exploiters cannot edit server scripts

2 Likes

This is what i knew, exploiters can edit only LocalScript.

The easiest way to do this without worrying about exploiters would be in a ServerScript, parented under the button

local player = script.Parent.Parent.Parent.Parent
local button = script.Parent
local Teams = game:GetService("Teams")
local Team = Teams.YourTeamName
button.MouseButton1Down:Connect(function()
	if player:IsInGroup(YourGroupId) then
		player.Team = Team
		player:LoadCharacter()
	end
end)
1 Like

Well, yes, I stated that in my reply. Local scripts work in PlayerGui, server scripts do not.

No you don’t. The server gets that automatically when you fire a remote event.

Remote events aren’t complicated. They are needed to make a functioning game. Please stop saying that it isn’t needed even though your code will fail to work.

This is the worst coding practice I’ve ever witnessed.

Server sided checks. It isn’t hard.

No. Remote events automatically get the player who fired the event.

Thank you.

Ok, I did that because I think it is the fast way to make it…just a thing…you can use your own practice and I my own.