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

How do I make coins for obby levels? [closed]

Asked by 4 years ago

How do I make a coin where every level you complete you get say 5 coins, but you cant get it again. PLEASE help I have been stuck on this for a while.

Closed as Not Constructive by BestCreativeBoy, LinavolicaDev, Rinpix, and DeceptiveCaster

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by
z8z 15
4 years ago

There a few ways you could do this. You could either tag all your checkpoints or put them in a folder and loop through them. For this example, we will be using the folder method, so it's easier for someone to understand.

First, we need to create the leaderstats so that we can add the coins to the value. (This will not save as I did not include a datastore)

--[[
    Script Type: Normal Script
    Parent: ServerScriptService
]]

-- services --
local players = game:GetService("Players")

-- script --
players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder", player) -- creates a folder and sets its parent to the new player
    leaderstats.Name = "leaderstats"

    local stage = Instance.new("NumberValue", leaderstats) -- new number value for the stages
    stage.Name = "Stage"

    local coins = Instance.new("NumberValue", leaderstats) -- new number value for the coins
    coins.Name = "Coins"

end)

Now that we have that let's get to the checkpoint system. We need to create a folder in the workspace named "Checkpoints" with the checkpoints inside of it. Each checkpoint's name should be set to whatever that stage level is.

--[[
    Script Type: Normal Script
    Parent: ServerScriptService
]]

-- services --
local players = game:GetService("Players")

-- locals --
local checkpointfolder = workspace:FindFirstChild("Checkpoints") -- find the checkpoint folder
local coinreward = 5 -- change this to whatever amount you'd like to give

if checkpointfolder then
    for index, checkpoint in pairs(checkpointfolder:GetChildren()) do
        if checkpoint:IsA("BasePart") then
            checkpoint.Touched:Connect(function(obj)
                local character = obj.Parent or obj.Parent.Parent

                if character:FindFirstChild("Humanoid") then
                    local playerobj = players:GetPlayerFromCharacter(character)

                    local leaderstats = playerobj:FindFirstChild("leaderstats")
                    local coins = leaderstats:FindFirstChild("Coins")
                    local stage = leaderstats:FindFirstChild("Stage")

                    if leaderstats then
                        if stage and coins then
                            if tonumber(checkpoint.Name) == stage.Value + 1 then -- next stage will always be greater by one
                                stage.Value = tonumber(checkpoint.Name) -- set their stage value to that stage
                                coins.Value += coinreward -- add 5 coins to their coins
                                print(("%s has moved onto the next stage and earned %s coins as a reward."):format(playerobj.Name, tostring(coinreward)))
                            end
                        end
                    end
                end
            end)

        end

    end

else
    return warn("Could not find the checkpoint folder")

end
Ad