Badge/Tool giver

Hello I’m working on making a script that games a player a badge and a tool when they join the game on a fixed day and does not work after that day but for those that joined on that day they would keep the tool that was given to them but I’m not a vary good scripter and i can’t seem to find and thing that could help me make something like this and even when i found something close to what i need it is not working if anyone could help me that would be great

Can you provide your code?

If I understand this correctly:
You want the players to keep a certain tool if they own a certain badge, or did you mean saving the tool so that they have it when they rejoin?

So you want the player to receive a badge and tool if they join on X day and keep that tool forever?

so i want a player to keep a certain tool if they own a certain badge i have were it gives a player a badge when they join but i don’t know how to make it give the player a tool if they own at badge and to make it so that they keep the tool forever

BadgeService | Roblox Creator Documentation check this out for awarding the badge, or if you don’t feel like learning do

local player = game.Players[“ROBLOX”] – Player name goes between the " "
local badgeId = – put the badge id here (the numbers in search bar on the badge page)
game:GetService(“BadgeService”):AwardBadge(player.UserId, badgeId)

and write that wherever/whenever you wanna award a certain player a badge

To do that, you’ll need to give them the badge first with BadgeService:AwardBadge(), then check if they have it with BadgeService:UserHasBadgeAsync().

BadgeService | Roblox Creator Documentation This will also be helpful for that, use it to check if the player has a certain badge

Remember that BadgeService is only accessible from a server script, attempting to award a badge from a local script will not work.

local players = game:GetService("Players")
local badgeService = game:GetService("BadgeService")
local badgeId = 0 --change to badge id
local tool = game.ServerStorage.Tool --change this to the correct location to the tool

players.PlayerAdded:connect(function(player)
	local currentDay = string.sub(os.time(), 1, 3)
	if currentDay == "Thu" then --thursday
		badgeService:AwardBadge(player.UserId, badgeId)
	end
	local hasBadge = badgeService:UserHasBadgeAsync(player.UserId, badgeId)
	if hasBadge then
		local toolClone = tool:Clone()
		toolClone.Parent = player.Backpack
	end
end)

Here’s a server script which will award a badge if the current day is Thursday, if it isn’t then the badge will not be awarded, the player is then checked for the badge & if they have the badge they are given a tool.

3 Likes

You should definitely loop through existing players in case the PlayerAdded function doesn’t fire in time, can be caused by delays and WaitForChild()'s:

local Players = game:GetService('Players')


local function PlayerAdded
(Player)
	print(('%s joined!'):format(Player.Name))
	-- Code.
end


-- // Check existing players
local GetPlayers = Players:GetPlayers()
for i = 1, (#GetPlayers)
do
	task.spawn(PlayerAdded, GetPlayers[i]) --// Run this function in parallel for every player.
end
Players.PlayerAdded:Connect(PlayerAdded)

The rest of it looks alright.

The PlayerAdded event always fires whenever a player joins, this would be unnecessary. There is also no present “WaitForChild()” call. Checking every other player every time a new player joins would be a waste of resources & you also risk breaking the BadgeService request limits.

As I said, unexpected delays and/or the usage of WaitForChild()'s above the function will cause it not to fire.

This might be rare, but can still happen, especially with WaitForChild.

There are no “WaitForChild()” commands, I’ve also never heard of or seen what you’re referring to happen ever. Even giving you the benefit of the doubt and assuming it does happen once a blue moon rejoining the server instance would suffice.

local players = game:GetService("Players")
local badgeService = game:GetService("BadgeService")
local badgeId = 0 --change to badge id
local tool = game.ServerStorage.Tool --change this to the correct location to the tool

players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local currentDay = string.sub(os.time(), 1, 3)
		if currentDay == "Thu" then --thursday
			badgeService:AwardBadge(player.UserId, badgeId)
		end
		local hasBadge = badgeService:UserHasBadgeAsync(player.UserId, badgeId)
		if hasBadge then
			local toolClone = tool:Clone()
			toolClone.Parent = player.Backpack
		end
	end)
end)

In fact a CharacterAdded event in this instance would be far superior, as a tool is being supplied which would be lost when the player first dies (if just awarded within a PlayerAdded event).

Please read the code carefully.

Also:

  1. IF WaitForChild()'s are being used above the PlayerAdded event, it will obviously delay it since it yields the thread. Not saying this script is using it.

  2. Unexpected delays can happen as well, however from my experience rare, but can still happen.

  3. The script loops through every existing player before connecting the event, in case players already has been added before the script runs.

You should do the same with CharacterAdded as well, as the character might be added before CharacterAdded can be called:


local Players = game:GetService('Players')


local function PlayerAdded
(Player)
	print(('%s joined!'):format(Player.Name))
	-- Code.
	
	local function CharacterAdded(Character)
		-- Code
	end
	local Character = Player.Character
	if (Character)
	then
		CharacterAdded(Character)
	end
	Player.CharacterAdded:Connect(CharacterAdded)
end


-- // Check existing players
local GetPlayers = Players:GetPlayers()
for i = 1, (#GetPlayers)
do
	task.spawn(PlayerAdded, GetPlayers[i]) --// Run this function in parallel for every player.
end
Players.PlayerAdded:Connect(PlayerAdded)

Can you stop copying & pasting my code? Would appreciate it, thanks.

and no, you’re adding unnecessary stress on the servers, in no circumstance have I ever heard of the CharacterAdded event failing to fire (again multiple respawns would mitigate any possible risk, even though there isn’t any).

Also you risk overstepping the limits imposed by the BadgeService service which would nullify whatever you’re trying to avoid anyway (even though you’re not avoiding anything in the first place).

You keep mentioning function calls which aren’t even present, please keep the thread on topic.

Can you stop copying & pasting my code? Would appreciate it, thanks.

I never copypasted your code, I copypasted my code to add the CharacterAdded event for you to understand.

and no, you’re adding unnecessary stress on the servers, in no circumstance have I ever heard of the CharacterAdded event failing to fire (again multiple respawns would mitigate any possible risk, even though there isn’t any).

Incorrect.


If you can’t seem to understand this, feel free to ask other developers who has experience in this field.
Also, this is now off-topic, so we should probably just leave it at that.

Regardless, I won’t bother to post more comments discussing this with you.
Feel free to fact-check this instead of arguing.

who has experience in this field.

All I needed to read.

local players = game:GetService("Players")
local badgeService = game:GetService("BadgeService")
local badgeId = 0 --change to badge id
local tool = game.ServerStorage.Tool --change this to the correct location to the tool

players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		local currentDay = string.sub(os.time(), 1, 3)
		if currentDay == "Thu" then --thursday
			badgeService:AwardBadge(player.UserId, badgeId)
		end
		local hasBadge = badgeService:UserHasBadgeAsync(player.UserId, badgeId)
		if hasBadge then
			local toolClone = tool:Clone()
			toolClone.Parent = player.Backpack
		end
	end)
end)

To the original poster, you’ll need to change the badgeId to the correct badgeId and the first three letters of the day to whatever day you would like the badge to be awarded on, you’ll also need to change the reference to the tool which you wish the player to receive if they have that badge.

is Tool after game.serverstorge the name of the tool?