This script is really working but when i reset its not visible again can somebody help me
pls help me im waiting its bugg
You’re giving the player the item before the character is loaded. Use player.CharacterAdded
and put the code under that instead.
The only time you’re checking if the player has the badge is upon first joining the game, as the function that checks this is activated from the PlayerAdded
event. In order to ensure the player’s Character receives the item each time they respawn, you can check for whenever their Character respawns with the CharacterAdded
event.
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local BadgeService = game:GetService("BadgeService")
local BADGE_ID = -- Badge ID goes here (screenshot is too hard to read)
local Red = ServerStorage.Red
local playersWithBadge = {}
--[[ This table will act as a cache for the players who have obtained the badge
so it reduces the amount of work that needs to be done when they respawn --]]
local function giveItem(Character) -- This function will be activated whenever the Character needs to be given the item
local Clone = Red:Clone()
Clone.Parent = Character
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character) -- Every time the player's Character respawns, this function will be activated to check if they have the badge
local success, playerHasBadge = pcall(function()
return BadgeService:UserHasBadgeAsync(player.UserId, BADGE_ID)
end)
if playersWithBadge[player.UserId] then -- If their UserId is in the table, then we know they have the badge
giveItem(Character)
else
if playerHasBadge then -- If the pcall function determines they have the badge, then...
playersWithBadge[player.UserId] = true -- We can add the player's UserId to the table
giveItem(Character) -- And activate the function which'll grant them the item
end
end
end)
end)
Edit: More info about some of the things included within the codeblock above can be found in the following Developer Hub pages
For future reference, instead of uploading a screenshot of the script, please format the text inside of a codeblock in your post. This will make it easier to read and see what’s going on.
For example, these few lines of text:
```
– Code goes here
```
Will end up looking like this:
-- Code goes here
i got red line from Player.Character The (Player) Got error Also The Local Red … the (local) got error or red Line
If you direct copy-pasted the codeblock, ensure that you’ve updated the variable for the BADGE_ID
– I left it blank because I was having a hard time reading the screenshot you posted.
Otherwise, it should be working fine. To make sure, I opened up Studio, placed it into a script, and added a number for the BADGE_ID
and no red underlines appeared.
This should work:
local BadgeService = game:GetService("BadgeService")
local BADGE_ID = 2124695926
local Red = game:GetService("ServerStorage"):WaitForChild("Red")
game:GetService("Players").PlayerAdded:Connect(funtion(player)
player.CharacterAdded:Connect(funtion(character)
if BadgeService:UserHasBadgeAsync(player.UserId, BADGE_ID) then
Red:Clone().Parent = character
end
end)
end)
You can also add players who owns badges to cache, as @StrongBigeMan9 did.
Thanks for helping me… it help me alot