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

Changing roles based on amount of coins?

Asked by 4 years ago

I'm making a game and I want it so when you have a certain amount of coins, you are on a certain team/role and you get a certain character when it happens. There is just one problem. I can't find a good way to make it so that when you have that amount of coins, you are that team. I've tried over and over again but it either is too buggy or doesn't work correctly or at all. Any help would be nice.

My script is in the server script service. Here is my script.

game.Players.PlayerAdded:Connect(function(player)
    while wait() do
    if player.leaderstats:WaitForChild('Coins').Value then
            local coins = player.leaderstats.Coins.Value
            if coins < 1000 then
                player.Team = game.Teams.Peasants
                wait()
                game.ReplicatedStorage.Ranks.Peasant.Name = "StarterCharacter"
                game.ReplicatedStorage.Ranks.StarterCharacter.Parent = game.StarterPlayer
                wait(2)
                game.StarterPlayer.StarterCharacter.Name = "Peasant"
                game.StarterPlayer.Peasant.Parent = game.ReplicatedStorage.Ranks
            elseif coins >= 1000 and coins < 10000 then
                player.Team = game.Teams.Villagers
                game.ReplicatedStorage.Ranks.Villager.Name = "StarterCharacter"
                game.ReplicatedStorage.Ranks.StarterCharacter.Parent = game.StarterPlayer
                game.StarterPlayer.StarterCharacter.Name = "Villager"
                game.StarterPlayer.Villager.Parent = game.ReplicatedStorage.Ranks
            elseif coins > 10000 then
                player.Team = game.Teams["High Class"]
                game.ReplicatedStorage.Ranks["High Class Villager"].Name = "StarterCharacter"
                game.ReplicatedStorage.Ranks.StarterCharacter.Parent = game.StarterPlayer
                game.StarterPlayer.StarterCharacter.Name = "High Class Villager"
                game.StarterPlayer["High Class Villager"].Parent = game.ReplicatedStorage.Ranks
            end
        end
    end
    player:LoadCharacter()
player.Character.Humanoid.Died:Connect(function()
    if player.leaderstats:WaitForChild('Coins').Value then
        local coins = player.leaderstats.Coins.Value
        if coins < 1000 then
            player.Team = game.Teams.Peasants
            wait()
            game.ReplicatedStorage.Ranks.Peasant.Name = "StarterCharacter"
            game.ReplicatedStorage.Ranks.StarterCharacter.Parent = game.StarterPlayer
            wait(2)
            game.StarterPlayer.StarterCharacter.Name = "Peasant"
            game.StarterPlayer.Peasant.Parent = game.ReplicatedStorage.Ranks
        elseif coins >= 1000 and coins < 10000 then
            player.Team = game.Teams.Villagers
            game.ReplicatedStorage.Ranks.Villager.Name = "StarterCharacter"
            game.ReplicatedStorage.Ranks.StarterCharacter.Parent = game.StarterPlayer
            game.StarterPlayer.StarterCharacter.Name = "Villager"
            game.StarterPlayer.Villager.Parent = game.ReplicatedStorage.Ranks
        elseif coins > 10000 then
            player.Team = game.Teams["High Class"]
            game.ReplicatedStorage.Ranks["High Class Villager"].Name = "StarterCharacter"
            game.ReplicatedStorage.Ranks.StarterCharacter.Parent = game.StarterPlayer
            game.StarterPlayer.StarterCharacter.Name = "High Class Villager"
            game.StarterPlayer["High Class Villager"].Parent = game.ReplicatedStorage.Ranks
        end
    end
    player:LoadCharacter()
    end)
end)

1 answer

Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
4 years ago

Use Tables And Use for loops to check the amount of coins and set the rank accordingly.

I didn't test this but hopefully this would work for you?

local Rankz = game.ReplicatedStorage:WaitForChild("Ranks")
local Ranks = { -- All The Ranks In The Game
    1000 = "Peasants",
    10000 = "Villagers",
    10001 = "High Class"
}

function returnRank(Coins) -- Finds The Rank From Amount Of Coins
    for Amount, Rank in pairs(Ranks) do
        if (Amount == 1000 and Coins < Amount) or (Amount == 10000 and Coins >= 1000 and Coins < Amount) or (Coins >= Amount) then
            return Rank
        end
    end
end

game.Players.PlayerAdded:Connect(function(Player) -- Fires When Player Joins
    local Character = Player.CharacterAdded:Wait() -- Gets The Character When Player Spawns
    local leaderstats = Player:WaitForChild("leaderstats") -- Waits For Leaderstats To Load
    local Coins = leaderstats:WaitForChild("Coins") -- Waits For Coins To Load
    local CurrentRank = returnRank(Coins.Value) -- Gets The Rank From Amount Of Coins
    Player.Team = game.Teams:FindFirstChild(CurrentRank) -- Sets The Players Team To Specified Team
    local RankClone = Rankz:FindFirstChild(CurrentRank):Clone() -- Clones The Teams Character Model (I Assume Its A Character Model)
    RankClone.Name = "StarterCharacter"
    RankClone.Parent = game.StarterPlayer -- Makes Character Model's Parent StarterPlayer
    wait(2)
    RankClone:Destroy()

    Coins.Changed:Connect(function() -- Fires Whenever Coins Value Is Changed
        CurrentRank = returnRank(Coins.Value) -- Gets The Rank From Amount Of Coins
        Player.Team = game.Teams:FindFirstChild(CurrentRank) -- Sets The Players Team To Specified Team
        RankClone = Rankz:FindFirstChild(CurrentRank):Clone() -- Clones The Teams Character Model (I Assume Its A Character Model)
        RankClone.Name = "StarterCharacter"
        RankClone.Parent = game.StarterPlayer -- Makes Character Model's Parent StarterPlayer
        wait(2)
        RankClone:Destroy()
    end)
    Character.Humanoid.Died:Connect(function() -- Fires Whenever the Player Dies
        CurrentRank = returnRank(Coins.Value) -- Gets The Rank From Amount Of Coins
        Player.Team = game.Teams:FindFirstChild(CurrentRank) -- Sets The Players Team To Specified Team
        RankClone = Rankz:FindFirstChild(CurrentRank):Clone() -- Clones The Teams Character Model (I Assume Its A Character Model)
        RankClone.Name = "StarterCharacter"
        RankClone.Parent = game.StarterPlayer -- Makes Character Model's Parent StarterPlayer
        wait(2)
        RankClone:Destroy()
    end)
end)

Ad

Answer this question