Hi, im making a find the freddys game and i want to make it so that you can buy a dev product to reveal a freddy but im having trouble with check if the player has the freddy already collected in the “FreddysCollected”. Someone help?
the current code:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local FreddysFolder = workspace.freddy
local MarketPlaceService = game:GetService("MarketplaceService")
local Button = script.Parent
local ID = 1616616474
local highlight = script:WaitForChild("Highlight")
Button.MouseButton1Up:Connect(function()
MarketPlaceService:PromptProductPurchase(plr, ID)
local success, errormessage = pcall(function()
MarketPlaceService.PromptProductPurchaseFinished:Connect(function(player, assetid, isPurchased)
if isPurchased == true then
local freddy = FreddysFolder:GetChildren()[math.random(1, #FreddysFolder:GetChildren())]
local highlightClone = highlight:Clone()
highlightClone.Parent = freddy
end
end)
end)
end)
local function checkIfFound(freddy)
for i,v in pairs(FreddysCollected:GetChildren()) do
if v.Name == freddy.Name then
return true
end
end
return false
end
I guess this will work?
Not sure how your game is setup once a freddy is collected.
If you are moving the freddys to a different folder via the server, this will apply to all players so I don’t recommend it.
If its saved on a table then disregard what i said above.
You need to save the data via a client so it only applies to the player in question.
You can then use a datastore to save/load that information.
Feels like you are making it more complicated then it should be.
Like I said before you can use a table to keep track all of the freddys that have not been found. (Populating the table once the game starts)
local freddy = {} -- Initialize the table to store Freddy data
-- Manually populate the table with Freddy names and boolean values
freddy["Freddy1"] = true
freddy["Freddy2"] = false
--...
You can then use a recursive function to find the values in the table, or you can just set the table element to nil, and move that element to another table called
freddysfound
and only randomly choose between the existing elements in the freddy table.
What do you mean by that?
If you are using what I said before, then you should hook up the purchase code with the freddys table, and only search through there to highlight a freddy yet to be found.
I said earlier to use another table called FreddysFound and move that element under there.
local Freddy = {} -- Initialize the table to store Freddy data
local FreddysFound = {} --keep track of found freddys
-- Manually populate the table with Freddy names and boolean values
Freddy["Freddy1"] = nil
FreddysFound["Freddy1"] = true
--...
If you want data to be saved across sessions, save the table into a datastore.
Not sure why you would want to go with that approach, but yeah that would work also.
If you want to do that, then the freddy data should be kept locally - maybe everytime a player joins create a folder based on the players data so you dont get confused - then access it from there.
For Example:
Game starts and a folder is created in replicatedstorage with the usersID as the name
under the userid folder, create 2 folders - freddys and freddysfound (freddys will contain every unfound object, and freddysfound will start empty)
everytime a freddy is found, move it to the freddysfound folder
you can continue to randomly choose objects in the freddys folder.
update, i made something that works, thanks for the help
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local FreddysFolder = workspace.Hitboxes
local MarketPlaceService = game:GetService("MarketplaceService")
local Button = script.Parent
local ID = 1616616474
local FreddysCollected = plr:WaitForChild("FreddysCollected")
local highlight = script:WaitForChild("Highlight")
local function checkIfFound(freddy)
for i,v in pairs(FreddysCollected:GetChildren()) do
if v.Name == freddy.Name then
return true
end
end
return false
end
Button.MouseButton1Up:Connect(function()
MarketPlaceService:PromptProductPurchase(plr, ID)
local success, errormessage = pcall(function()
MarketPlaceService.PromptProductPurchaseFinished:Connect(function(player, assetid, isPurchased)
if isPurchased == true then
local availableFreddys = {}
for _, freddy in ipairs(FreddysFolder:GetChildren()) do
if not checkIfFound(freddy) then
table.insert(availableFreddys, freddy)
end
end
if #availableFreddys > 0 then
local selectedFreddy = availableFreddys[math.random(1, #availableFreddys)]
local highlightClone = highlight:Clone()
highlightClone.Parent = selectedFreddy
print(selectedFreddy)
else
print("All Freddy characters have been collected.")
end
end
end)
end)
end)