Hello, dear community members,
I’m currently working on a game called “Cookie Simulator”. In-game, players must collect cookies by stepping on them. When a player touches a cookie, they receive a cookie via the leaderstats.
Notes: The cookies (the parts) are in a folder, which is in Workspace.
The problem is: When I touch a cookie, the script is running on the first cookie in the folder only.
Here’s the script:
-- Settings
local Cookies = game.Workspace.Maps.Spawn.Cookies:GetChildren()
local MarketplaceService = game:GetService("MarketplaceService")
local GamepassId = 8270530
local Player = game.Players.LocalPlayer
local CookieToAdd = script.Value.Value
-- Functions
for _, Cookie in pairs (Cookies) do
function Invisible()
Cookie.CanCollide = false
for i = 0, 1, .1 do
wait()
Cookie.Transparency = i
end
end
function Visible()
Cookie.CanCollide = true
for i = 1, 0, -.1 do
wait()
Cookie.Transparency = i
end
end
function onTouched(Hit)
local Enabled = false
if Enabled == false then
Enabled = true
local CheckHumanoid = Hit.Parent:FindFirstChild("Humanoid")
if CheckHumanoid then
local User = game.Players:GetPlayerFromCharacter(Hit.Parent)
local Stats = User:findFirstChild("leaderstats")
Invisible()
if Stats then
local CookiesStats = Stats:findFirstChild("Cookies")
if MarketplaceService:UserOwnsGamePassAsync(User.UserId, GamepassId) then
CookiesStats.Value = CookiesStats.Value + CookieToAdd * 2
else
CookiesStats.Value = CookiesStats.Value + CookieToAdd * 1
end
end
game.ReplicatedStorage.Events.CookieCollected:FireClient(User)
wait(10)
Visible()
Enabled = false
end
end
end
Cookie.Touched:connect(onTouched)
end
How can I fix this problem? Thanks for your future help.
~Arlenox!