I Have A Level GUI But I Dont Know How To Make a Script When You Touch The Part You Get Exp

My Problem Is That I Want To Make A Leveling System And You Get Levels By Stepping On A Part.

  1. here’s my GUI:image
    I Just Need To Make A Script When You Step On A Part The Bar Goes Up.
    This Is Not That Detailed But Please Help Me Lol.

So if you wanna make a Part that gives XP when touched, let’s start with making our variables first :thinking:

local AmountToGive = 50
local Cooldown = false

These 2 are pretty straightforward, AmountToGive is the amount of XP you want to give the specific player, and Cooldown will prevent anyone from getting rapid amounts of XP so we set it as false for now

Next, let’s define our function here:

local function TouchedMoment(HitPart)

end

script.Parent.Touched:Connect(TouchedMoment)

The first argument of a Touched Event is the HitPart, or in simple terms: The part that it touched which activated the Function

Next, we’re connecting the function to the Touched event (Or the TouchedMoment function)

Now, we’ll go ahead and create our define our Player variable inside the function:

local Player = game.Players:GetPlayerFromCharacter(HitPart.Parent)

Simple, we’re getting the Player object from the GetPlayerFromCharacter function (Remember in this instance, that HitPart.Parent is referring to the HitPart that it’s parented to or the Character)

Next following up after we define the Player variable:

if Player and Cooldown == false then
    Cooldown = true
    Player.leaderstats.XP.Value += AmountToGive
    wait(5)
    Cooldown = false
end

Now here, we’re using a if then statement to check if the Player Object is there & our Cooldown is defined as false, if both of those have met the correct requirements then we can continue on with our code! First thing we’ll do is set the Cooldown variable to true:

Cooldown = true
Player.leaderstats.XP.Value += AmountToGive

(Assuming that we hopefully have the Player’s leaderstats) we can increase the Player’s XP value with what amount the AmountToGive variable is, which is 50

wait(5)
Cooldown = false

After 5 seconds have passed, we can set back the Cooldown variable back to false

So in summary, here is the code in full form!

local AmountToGive = 50
local Cooldown = false

local function TouchedMoment(HitPart)
    local Player = game.Players:GetPlayerFromCharacter(HitPart.Parent)
    if Player and Cooldown == false then
        Cooldown = true
        Player.leaderstats.XP.Value += AmountToGive
        wait(5)
        Cooldown = false
    end
end

script.Parent.Touched:Connect(TouchedMoment)

This took way more than 10 minutes to write send help

3 Likes