Find the freddys Help

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)
2 Likes

Forgot to add the variable for the freddyscollected folder but its located in the player

2 Likes

You can create a function to check all the elements in the folder if they have been found / claimed already.

You can save the already found freddys in a table, or you can set the attribute for the models to be “found” and skip over them.

Make sure that if all freddys have been found, the player cannot buy this developer item.

3 Likes

I would hook up a remote function that sends the purchase to the server instead of the client. It prevents hackers and may fix it.

1 Like

would this function work?

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
1 Like

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.

I can explain it for you, basically there is a folder in replicated storage which looks like this
image
when a player touches one of these hitboxes
image
the corresponding BoolValue will be Cloned into a folder inside of the “FreddysCollected” Folder

My code works, all i have to do is use this

if checkIfFound(freddy) == false then
	highlightClone.Parent = freddy
end

but now i dont know how to re-roll until it finds one that isnt already claimed

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.

but how would i know if the players have found the freddys before hand?

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.

would it work if the tables are substituted for folders? since that is how it is set up

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:

  1. Game starts and a folder is created in replicatedstorage with the usersID as the name
  2. under the userid folder, create 2 folders - freddys and freddysfound (freddys will contain every unfound object, and freddysfound will start empty)
  3. everytime a freddy is found, move it to the freddysfound folder
  4. you can continue to randomly choose objects in the freddys folder.

also if you didnt see it i explained how the freddys are collected earlier

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)

You can shorten your checkIfFound function greatly by using :FindFirstChild.

Code:

local function checkIfFound(freddy)
	return FreddysCollected:FindFirstChild(freddy.Name)
end
1 Like