Hello! I am having trouble with awarding badges to player when they pick up an item that they don’t already have in a folder.
I was trying to make it so when a player finds a new melon, they will get a UI pop up and get awarded the badge with the badge of that melon. In contrast, if they already have the melon, they would get a notification saying that they already have the melon.
How it works is when player walked into a new melon, a clones value will be put into the “melonsCollected” folder and save it there. If the value is there, the player already have the melon, if it’s not, the player will be awarded the badge and the award GUI will pop up
But when I played, and resetted all my data and walk into a melon i don’t have, it passed it onto the “alreadyHave” event instead of awarding player the badge and make the GUI pop up.
here is the main script:
local melonsFolder = script.Parent
local replicatedStorage = game:GetService("ReplicatedStorage")
local melonValuesFolder = replicatedStorage:FindFirstChild("MelonValues")
local BS = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeEvent = replicatedStorage.RemoteEvents.badgeGiver
local newCollectedEvent = replicatedStorage.RemoteEvents.newCollected
-- Function to handle the touch event
local function onPartTouched(other, melonPart)
-- Check if the thing that touched the part is a player's character
local player = game.Players:GetPlayerFromCharacter(other.Parent)
if player then
-- Get the name of the part that was touched
local touchedPartName = melonPart.Name
-- Find the corresponding value in the MelonValues folder
local valueToClone = melonValuesFolder:FindFirstChild(touchedPartName)
if valueToClone then
-- Clone the value and parent it to the target folder
if player:WaitForChild("melonsCollected"):FindFirstChild(melonPart.Name) then return end
local targetFolder = player:WaitForChild("melonsCollected")
local clonedValue = valueToClone:Clone()
clonedValue.Parent = targetFolder
clonedValue.Value = true
end
end
end
function debounce(func)
local isRunning = false
return function(hit)
if not isRunning then
isRunning = true
func(hit)
wait(3)
isRunning = false
end
end
end
local function giveBadge(player, badgeid, imageid, name)
local success, badgeInfo = pcall(function()
return BS:GetBadgeInfoAsync(badgeid)
end)
if success then
if badgeInfo.IsEnabled then
local success, result = pcall(function()
newCollectedEvent:FireClient(player, badgeid, imageid, name)
print(badgeid)
return BS:AwardBadge(player.UserId, badgeid)
end)
print("passed")
if not result then
badgeEvent:FireClient(player, badgeid, imageid)
end
end
end
end
-- Connect the touch event to all parts in the Melons folder
for _, part in ipairs(melonsFolder:GetChildren()) do
if part:IsA("BasePart") then -- Ensure the instance is a part
part.Touched:Connect(debounce(function(other)
local player = game.Players:GetPlayerFromCharacter(other.Parent)
onPartTouched(other, part)
if player then
giveBadge(player, part.BadgeID.Value, part.MainDecal.Texture, part.Name)
end
end))
end
end
Here is the remoteEvents manager:
local replicatedStorage = game:GetService("ReplicatedStorage")
local melonValuesFolder = replicatedStorage:FindFirstChild("MelonValues")
local BS = game:GetService("BadgeService")
local Players = game:GetService("Players")
local badgeEvent = replicatedStorage.RemoteEvents.badgeGiver
local newCollectedEvent = replicatedStorage.RemoteEvents.newCollected
local StarterGUI = game:GetService("StarterGui")
local mushroomAwarded = StarterGUI:WaitForChild("FoundGUI")
local frame = mushroomAwarded:WaitForChild("Frame")
local melonName = frame:WaitForChild("Name")
local melonImage = frame:WaitForChild("Image")
badgeEvent.OnClientEvent:Connect(function(player, imageid)
StarterGUI:SetCore("SendNotification", {
Title = "Uh Oh!",
Text = "You've already collected this melon!🍈🍉",
Duration = 3,
Icon = imageid
})
end)
newCollectedEvent.OnClientEvent:Connect(function(player, imageid, name)
melonImage.Image = imageid.."!"
melonName.Text = name
frame.Visible = true
wait(5)
frame.Visible = false
end)
Here is a video for visual:
Help are appreciated!!!