I am currently making a Find The game where you have to find memes. Whenever you touch a meme, a GUI pops up, saying you collected it and places the meme in a Folder parented to the player. However, I’m trying to make it where the GUI won’t come up if the player has already collected the meme. I tried to run this code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local NewMemeCollected = RemoteEvents:WaitForChild("NewMemeCollected")
local Frame = script.Parent
local MemeName = Frame:WaitForChild("MemeName")
local MemeImage = Frame:WaitForChild("MemeImage")
local Memes = game.Workspace.Memes
local collectedMemes = {}
for _, meme in ipairs(Memes:GetChildren()) do
if meme:IsA("Part") then
collectedMemes[meme.Name] = false
end
end
NewMemeCollected.OnClientEvent:Connect(function(name,ImageId,meme)
if collectedMemes[meme.Name] == false then
MemeName.Text = name.." "
script.Parent.CollectionSound:Play()
MemeImage.Image = ImageId
Frame.Visible = true
wait(4)
Frame.Visible = false
end
end)
Whenever you touch an already collected meme, I get this error: Players.TabbyCat904.PlayerGui.NewMemeCollectedGUI.Frame.NewlyCollectedMeme:20: attempt to index nil with ‘Name’.
If you think you know how to fix this, please let me know. Thank you in advance for your help.
I see a problem. You’re trying to access meme, but meme is in an entirely different scope. I’d suggest using attributes and adding attributes to the character when they collect the meme, and then check if the attribute is there or not to decide if the GUI should appear or not.
But if you want to keep your original structure. Do another table loop with the if statement inside of it, but also keep it inside the event.
Did I do it right? I’m not sure if what I did with the else statement is correct or not.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local NewMemeCollected = RemoteEvents:WaitForChild("NewMemeCollected")
local Frame = script.Parent
local MemeName = Frame:WaitForChild("MemeName")
local MemeImage = Frame:WaitForChild("MemeImage")
local Memes = game.Workspace.Memes
local collectedMemes = {}
NewMemeCollected.OnClientEvent:Connect(function(name,ImageId,meme)
for _, meme in ipairs(Memes:GetChildren()) do
if meme:IsA("Part") then
collectedMemes[meme.Name] = false
Frame.Visible = false
else
MemeName.Text = name.." "
script.Parent.CollectionSound:Play()
MemeImage.Image = ImageId
Frame.Visible = true
wait(4)
Frame.Visible = false
end
end
end)
You were close, but that’s not what I mean, this is what I mean:
for _, meme in ipairs(Memes:GetChildren()) do
if meme:IsA("Part") then
collectedMemes[meme.Name] = false
end
end
NewMemeCollected.OnClientEvent:Connect(function(name,ImageId,meme)
for _, meme in ipairs(Memes:GetChildren()) do
if collectedMemes[meme.Name] == false then
end
end
end)
Sorry for the (really) late reply lol. Ignore the comments.
local BadgeService = game:GetService("BadgeService")
local meme = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local BadgeCollected = Remotes:WaitForChild("BadgeCollected")
local NewMemeCollected = Remotes:WaitForChild("NewMemeCollected")
local name = "Baller" -- Change to the right meme name
local ImageId = "rbxassetid://11151804223" -- Change to the right meme decal Id
function Debounce(func)
local isRunning = false
return function(hit)
if not isRunning then
isRunning = true
func(hit)
task.wait(5)
isRunning = false
end
end
end
local function GiveBadge(Player, BadgeId)
local success, BadgeInfo = pcall(function()
return BadgeService:GetBadgeInfoAsync(BadgeId)
end)
if success then
if BadgeInfo.IsEnabled then
local success, result = pcall(function()
NewMemeCollected:FireClient(Player, name, ImageId)
return BadgeService:AwardBadge(Player.UserId, BadgeId)
end)
if not result then
BadgeCollected:FireClient(Player)
end
end
end
end
meme.Touched:Connect(Debounce(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local character = hit.Parent
local Player = game.Players:GetPlayerFromCharacter(character)
if Player then
GiveBadge(Player, 2153448135) --Change the second parameter to the right BadgeId
end
end
end))
I finally figured out how to do this! I rescripted the entire thing, and just added a check that scanned the Player’s inventory for the name of meme that was just collected.
Thank you everyone for your help! Have a wonderful day!