Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
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)

0
Local Script or Server Script? MythicalShade 420 — 5y
0
Server script, it is for a team changer tictac67 96 — 5y
0
And the script is located under a textButton/ImageButton?? MythicalShade 420 — 5y
0
Yes, a text button tictac67 96 — 5y
View all comments (3 more)
0
Alright give me a minute to write my answer. MythicalShade 420 — 5y
0
It also needs to work with FE tictac67 96 — 5y
0
I know. MythicalShade 420 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
Can I do it for a table using UserIds? so if i put [1235332] = true it would let me change? tictac67 96 — 5y
0
Yeah I'll edit the script and show how MythicalShade 420 — 5y
Ad

Answer this question