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

this script won't make inventory invisible for certain team??

Asked by 1 year ago

Hi I am trying to code away that if a player is on team citizen the inventory goes invisible script in serverscriptservice

local Players = game:GetService("Players")



local Teams = game:GetService("Teams")



while wait() do



if Players.Team "Citizen" then



    game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) 



    end



    end

3 answers

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

You'll gonna need to use a RemoteEvent for this. Make sure you have a Script in ServerScriptService, a LocalScript in StarterGui, and a RemoteEvent named EnableBackpack in ReplicatedStorage.

To detect if a play joins a Team, you use Team.PlayerAdded and Team.PlayerRemoved when a player leaves the Team.

Script in ServerScriptService:

local Teams = game:GetService("Teams")
local CitizenTeam = Teams.Citizen

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EnableBackpack = ReplicatedStorage.EnableBackpack

CitizenTeam.PlayerAdded:Connect(function(player) -- when a player joins this team
    EnableBackpack:FireClient(player, false) -- disable backpack
end)

CitizenTeam.PlayerRemoved:Connect(function(player) -- when a player leaves this team (player left/player's team set to neutral/player joined a new team)
    pcall(function() -- we use a pcall incase the player left to avoid errors (errors break the script, and maybe the whole game!!)
        EnableBackpack:FireClient(player, true) -- enable backpack
    end)
end)

LocalScript in StarterGui:

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

EnableBackpack.OnClientEvent:Connect(function(enabled)
    local success
    repeat
        -- we will use pcall() to avoid errors
        success = pcall(function()
            StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, enabled)
        end)
    until success -- loops the code until backpack was finally enabled/disabled
end)
0
It works thanks!! theking66hayday 841 — 1y
Ad
Log in to vote
1
Answered by 1 year ago

You're checking Players(the service) rather than each individual player. Try looping through each player in the server and checking their team instead.

while wait() do

    for i,v in pairs(Players:GetPlayers()) do

        --Check team and possibly disable gui

    end

end
0
No errors and it does nothing still theking66hayday 841 — 1y
Log in to vote
1
Answered by
pwx 1581 Moderation Voter
1 year ago

You should not use a Script in ServerScriptService for this since the server cannot access their client, you can do all of this in the client. Use a LocalScript in StarterCharacterScripts.

local Players = game:GetService('Players')
local Teams = game:GetService('Teams')
local starterGui = game:GetService('StarterGui')
local Player = Players.LocalPlayer

if Player.Team == Team['Citizen'] then
    starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
else
    starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
end
0
This didn't work no errors either theking66hayday 841 — 1y

Answer this question