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

Touch for XP Block (LevelingSystem)?

Asked by 3 years ago

Hey guys! So I have another question is that I have a IntValue LevelingSystem. I have made a rectangular part and putted this script inside of it:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
    plr.LevelingSystem.XP.Value = plr.LevelingSystem.XP.Value + 5
    wait(0.1)
end)

This script gives you XP when you click on it. This is a very easy question, but I am not that good at lua so, my question is how to make it so that if you TOUCH it, you will get points.

(BONUS: If you know how can you tell me how I can make a leaderstat that shows my IntValue?

2 answers

Log in to vote
0
Answered by 3 years ago

That's simple. You should you Touched event. You can learn more about it here.

local giveXP = true
script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- we get player from the parts that touch the xp block
    if player then -- make sure it is a player and not other random parts
        if giveXP then -- check for cooldown
            giveXP = false
            player.LevelingSystem.XP.Value += 5 -- += is the same as player.LevelingSystem.XP.Value = player.LevelingSystem.XP.Value + 5 but shorter

            wait(.1) -- how long the cooldown should be
            giveXP = true
        end
    end
end)

Hope this helps! If there's any error, let me know and accept if there isn't any!

Ad
Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
3 years ago

Addition to Xviperlink's answer:

if you want it to look like you pick up the xp block and it respawns, do this instead:

local giveXP = true

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent) 
    if player then 
        if giveXP then 
            giveXP = false -- enables the cooldown
            player.LevelingSystem.XP.Value += 5
            script.Parent.Transparency = 1 -- makes it invisible
            script.Parent.CanCollide = false -- makes it lit untouchable

            wait(.1) -- how long the cooldown should be
            giveXP = true -- disables the cooldown
            script.Parent.Transparency = 0 -- makes it visible
            script.Parent.CanCollide = true -- makes you able to touch it
        end
    end
end)

Answer this question