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

How to disable the Backpack based on Team?

Asked by 1 year ago
Edited 1 year ago

Info; When the player is in the lobby they are transferred to the "Lobby" Team, when they are in the map they are transferred to the "Playing" Team. I would like for my script to disable the players backpack when they are in the Lobby, otherwise they can pull out their weapons and kill other players, which could stop them from loading into the map.

My current script is a local-script that is placed into StarterGui.

script:

game.Players.PlayerAdded:Connect(function(player)
    local theTeam = player.Team.Name
    if theTeam == "Playing" then
        game:GetService("StarterGui"):SetCoreGuiEnabled("Backpack", true)
    elseif theTeam == "Lobby" then
        game:GetService("StarterGui"):SetCoreGuiEnabled("Backpack", false)
    end
end)

I didn't really know what I was doing lol.

2 answers

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

I think the problem here is that you're only checking the team of the player once when the player is joining. I recommend using GetPropertyChangedSignal("Team"), for example:

local Teams = game:GetService("Teams")
local Backpack = Enum.CoreGuiType.Backpack -- Usually I wouldn't do this, but for demonstration purposes I used a variable for the enum
local player = game:GetService("Players").LocalPlayer

local function check()
    if player.Team == Teams.Playing then
        game:GetService("StarterGui"):SetCoreGuiEnabled(Backpack, true)
    elseif player.Team == Teams.Lobby then
        game:GetService("StarterGui"):SetCoreGuiEnabled(Backpack, false)
    end
end

check() -- Check once when the player joins
player:GetPropertyChangedSignal("Team"):Connect(check) -- Check every time the player's team changes
0
This is almost correct, but you need to disable the backpack in LocalScript T3_MasterGamer 2189 — 1y
0
This is a `LocalScript` already, it works properly if you put it in `StarterGui` or `StarterPlayerScripts`. DindinYT37 246 — 1y
0
Thanks man for taking time to help some random guy on the internet. Rancidladyfingers2 7 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

@DindinYT37's answer is almost correct, but he forgot that it should be used in the Client. (LocalScripts, ModuleScript required by a LocalScript, etc.)

To communicate with the Client on the Server, we can use a RemoteEvent. First, create a RemoteEvent inside ReplicatedStorage. Lastly, name it EnableBackpack.

Also @DindinYT37 you don't need to check when a Player joins the game, you can use Team.PlayerAdded when a Player joins the Team.

Server side (Script inside ServerScriptService or Workspace):

local Teams = game:GetService("Teams")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Playing = Teams.Playing
local Lobby = Teams.Lobby

local EnableBackpack = ReplicatedStorage.EnableBackpack -- the RemoteEvent we'll use

Playing.PlayerAdded:Connect(function(newTeammate) -- when a player joins the team
    pcall(function()
        EnableBackpack:FireClient(newTeammate, true) -- if the player leaves/disconnects, it might error so we will do it in a pcall() to avoid errors
    end)
end)

Lobby.PlayerAdded:Connect(function(newTeammate) -- when a player joins the team
    pcall(function()
        EnableBackpack:FireClient(newTeammate, false) -- if the player leaves/disconnects, it might error so we will do it in a pcall() to avoid errors
    end)
end)

Client side (LocalScript inside StarterPlayerScripts or StarterGui):

local StarterGui = game:GetService("StarterGui")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local EnableBackpack = ReplicatedStorage:WaitForChild("EnableBackpack") -- the RemoteEvent we'll use

EnableBackpack.OnClientEvent:Connect(function(backpackEnabled)
    local success
    repeat
        success = pcall(function()
            StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, backpackEnabled) -- disables the backpack
        end)
        task.wait()
    end
end)
0
This can also be achieved way easier by using a `LocalScript` in `StarterGui` or `StarterPlayerScripts`, as my answer mentioned. Also, it is already the client. DindinYT37 246 — 1y

Answer this question