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

How do i get this to work with all the names in the array?

local button = script.Parent
local plr = script.Parent.Parent.Parent.Parent.Parent
local player = script.Parent.Parent.Parent.Parent
local names = {"tictac67"}
button.MouseButton1Click:Connect(function()
	if plr.Name == "tictac67" then
		plr.TeamColor = game.Teams["Security Department"].TeamColor
	    game.Workspace[plr.Name].Humanoid.Health = 0
	end
end)

7 comments

0

Local Script or Server Script?

0

Server script, it is for a team changer

0

And the script is located under a textButton/ImageButton??

0

Yes, a text button

0

Alright give me a minute to write my answer.

0

It also needs to work with FE

0

I know.

1 answer

First of all, buttons (or anything GUI related) can not be handled by a server script, only a local script. Note that they do work in play solo but not in a real game. So your original script would have to be changed to a local script.

Next, now that the script is a local script, that means that changing the player's team would only replicate to the client making the change, instead of all clients and the server, meaning that you need to use a remoteEvent.

First off, you need to create a RemoteEvent in replicated storage, then a server side script in server script service.

In the local script parented to the TextButton, write the following code.

local event = game:GetService('ReplicatedStorage').RemoteEvent
local button = script.Parent
local plr = game:GetService("Players").LocalPlayer -- much easier to get player (only works in local script)
 -- Didn't know why there were two player variables so I removed one
local IDs = {"26331361"}
button.MouseButton1Click:Connect(function()
	for i,v in pairs(IDs) do
		if plr.UserId == "26331361" then
			local teamName = "Security Department"
			event:FireServer(teamName)
		else
			local teamName = "Losers Department"
			event:FireServer(teamName)
  		end	
	end
end)

In the server script located in ServerScriptService,

local event = game:GetService('ReplicatedStorage').RemoteEvent

event.OnServerEvent:Connect(function(ignore,teamName,SelectedPlayer) -- ignore is the argument that is automatically passed (so ignore it!)
	SelectedPlayer.TeamColor = game.Teams[teamName].TeamColor
	if not SelectedPlayer.Character then
		repeat wait() until SelectedPlayer.Character
		SelectedPlayer.Character.Humanoid.Health = 0
	end
end)

This should work as long as the teams exist and the remoteEvent exists in replicated storage. Let me know if any problems arise.

2 comments

0

Can I do it for a table using UserIds? so if i put [1235332] = true it would let me change?

0

Yeah I'll edit the script and show how

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