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 4 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:

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

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 4 years ago

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

01local giveXP = true
02script.Parent.Touched:Connect(function(hit)
03    local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- we get player from the parts that touch the xp block
04    if player then -- make sure it is a player and not other random parts
05        if giveXP then -- check for cooldown
06            giveXP = false
07            player.LevelingSystem.XP.Value += 5 -- += is the same as player.LevelingSystem.XP.Value = player.LevelingSystem.XP.Value + 5 but shorter
08 
09            wait(.1) -- how long the cooldown should be
10            giveXP = true
11        end
12    end
13end)

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
4 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:

01local giveXP = true
02 
03script.Parent.Touched:Connect(function(hit)
04    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
05    if player then
06        if giveXP then
07            giveXP = false -- enables the cooldown
08            player.LevelingSystem.XP.Value += 5
09            script.Parent.Transparency = 1 -- makes it invisible
10            script.Parent.CanCollide = false -- makes it lit untouchable
11 
12            wait(.1) -- how long the cooldown should be
13            giveXP = true -- disables the cooldown
14            script.Parent.Transparency = 0 -- makes it visible
15            script.Parent.CanCollide = true -- makes you able to touch it
16        end
17    end
18end)

Answer this question