Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Touch for XP Block (LevelingSystem)?

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

✓ Accepted answer

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!

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.