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

How can I make a team only available to people who played ex. 15+ minutes?

Asked by 2 years ago

I want to make a team only available to people that played ex. 15+ minutes. How can I do that? If someone knows how, please tell me.

2 answers

Log in to vote
0
Answered by 2 years ago

Well, you gotta be more specific, how do you pretend to change the player's team?

First of all, assuming you already have a way of tracking how long has a player been playing for You just need to use "player.Team", and "If"

For example In this script, we change the player's team if their "leaderboard points" are higher than 15 And the script runs when the player touches a brick, which would be the change team brick

function A(h)
    local char = h.Parent
    local plr = game.Players:GetPlayerFromCharacter(char)

    if plr.leaderstats.Time.Value >= 15 then
        plr.Team = game.Teams.Janitor
    end
end


script.Parent.Touched:Connect(A)

If you still don't understand, please be more specific so I can provide a better script

0
That is very inefficient . Hieroglyphica 0 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

<?> Okay, so what I understand is that you want to see if a player has played your 15+ minutes. If so, I wrote this quick code that will save the player progress as well as put a number value inside the player's instance.

Now I didn't put it as minutes but instead second, this way it's easier to differentiate the time. To calculate seconds into minutes, use any calculator or just an online converter website.

15 minutes into seconds: 900 seconds.

  • Remember to enable API access. (To enable you go to: Game Settings => Security => Enable Studio Access To API Services). Which is for data storage.

  • Server Script:

local Players = game:GetService("Players");

local DataStoreService = game:GetService("DataStoreService")
local DataStore = {
    ["DataStore"] = DataStoreService:GetDataStore("PlayerTime");

    StartTimer = function(_, PlayerInstance)
        local PlayerCurrentTime;
        local success, error = pcall(function()
            PlayerCurrentTime   = _.DataStore:GetAsync(PlayerInstance.UserId); -- Checks if the player has any data.
        end)

        if PlayerCurrentTime == nil then -- If the player hasn't been added to the datastore beforehand it will return nil, as noted.
            PlayerCurrentTime = 0; -- This will put a default number to anyone who hasn't played before or was removed from the datastore.

            -- This can be done with setmetable but in your case, it's only one value.
        end

        local TimeValue = Instance.new("NumberValue", PlayerInstance);
        TimeValue.Name = "Time";
        TimeValue.Value = PlayerCurrentTime;

        if success then
            local Time = PlayerCurrentTime;

            while wait(1) and PlayerInstance do -- Counts per second, this will go on until the player leaves.
                Time += 1;

                TimeValue.Value = Time;
            end
        else
            warn(error);
        end
    end,
    SaveTimer = function(_, PlayerInstance)
        local PlayerTime = PlayerInstance:FindFirstChild("Time").Value;

        local success, error = pcall(function()
            _.DataStore:SetAsync(PlayerInstance.UserId, PlayerTime); -- Checks if the player has any data.
        end)

        if success then
            print(string.format("Successfully saved, %s's data!", PlayerInstance.Name))
        else
            warn(error);
        end
    end,
};

Players.PlayerAdded:Connect(function(PlayerInstance)
    DataStore:StartTimer(PlayerInstance);
end)

Players.PlayerRemoving:Connect(function(PlayerInstance)
    DataStore:SaveTimer(PlayerInstance);
end)
  • To make a player change team depending on the players time.

<?> Okay, first make an RemoteEvent called RequestTeam inside ReplicatedStorage. Then, put a number value inside your team called TimeAmount inside that value you will put the desired amount to join that team, in your case 900 seconds(15m).

If the player matches the value inside the team then it will assign that team the player requested.

  • RemoteEvent's can be called from any script, if locally then it would be: game:GetService("ReplicatedStorage").RequestTeam:FireServer("yourteam");

  • Server Script:

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

local Events = {};
Events.RequestTeam = ReplicatedStorage.RequestTeam;

Events.RequestTeam.OnServerEvent:Connect(function(PlayerInstance, RequestedTeam)
    local PlayerTime = PlayerInstance:FindFirstChild("Time").Value;

    if RequestedTeam and Teams:FindFirstChild(RequestedTeam) then
        local TeamTime = Teams[RequestedTeam]:FindFirstChild("TimeAmount").Value;

        if PlayerTime >= TeamTime then
            PlayerInstance.Team = Teams[RequestedTeam];
        end
    end
end)

Answer this question