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

How do you make a script so that when you click a block, it blows you up AND gives you a badge?

This issue is so oddly specific that I wasn't able to find the solution anywhere else, what's wrong with my script?


local clicked = script.Parent.ClickDetector.MouseClick
local part = script.Parent
local debounce = false
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local IDToAward = 0 -- There's an actual ID here.

clicked:Connect(function(player)
	if not debounce then
		debounce = true

		local explosion = Instance.new("Explosion")
		explosion.Position = player.Character:WaitForChild("HumanoidRootPart").Position
		explosion.Parent = workspace
		
		debounce = false
		
		if not BadgeService:UserHasBadgeAsync(Players.UserId, IDToAward) then		
        BadgeService:AwardBadge(Players.UserId, IDToAward)
		
		
		end
			script.Disabled = true
		end
end)

1 answer

I deleted the last post I made and made a new one so that you could get the notification (Yea, I didn't bother to edit it), I fixed the script and it should be working if done properly: (Tested and working)

local Detector = script.Parent.ClickDetector
local part = script.Parent
local debounce = false
local BadgeService = game:GetService("BadgeService")
local Players = game:GetService("Players")
local IDToAward = 0 -- There's an actual ID here.

local debTable = {} -- Since we're using a ServerScript for this, tables are more effective for debouncing

Detector.MouseClick:Connect(function(player)
	if not debTable[player.Name] then
		debTable[player.Name] = true
		
		local Success, plrOwnBadge = pcall(function() -- More secure to use pcall in case if script errors
			return BadgeService:UserHasBadgeAsync(player.UserId, IDToAward) -- In your script, you were addressing to the Player Service instead of the client itself
		end)

		if Success then
			if not plrOwnBadge then
				local explosion = Instance.new("Explosion")
				explosion.Position = player.Character:FindFirstChild("HumanoidRootPart").Position -- The character should be loaded by the time they clicked on this, unless if they click it in like a blink
				explosion.Parent = workspace

				BadgeService:AwardBadge(player.UserId, IDToAward)
				task.wait(2)
				debTable[player.Name] = nil
			end
		end
		-- script.Disabled = true | I'm not sure what's the purpose of this line? It deletes for the whole server.
	end
end)

1 comment

0

Alright, this worked! Thank you so much for your time.

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