Why do i get this error?

the error is

08:05:47.334 - Workspace.ClickPart.ClickDetector.Script:62: Expected ')' (to close '(' at line 3), got 'end'

script is

local clickDetector = script.Parent

game.Players.PlayerAdded:Connect(function(player)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataModule = require(ReplicatedStorage:WaitForChild("DataModule"))
local Gifts = ReplicatedStorage:WaitForChild("Gifts")

local function ChooseGift(player)
	local PlayerGui = player:WaitForChild("PlayerGui")
	local MainGui = PlayerGui.MainGui
	local GiftIcon = MainGui.GiftIcons
	local GiftImages = MainGui.GiftImages
	
	local timer = tick()
	
	GiftIcon.Visible = true
	
	while tick() - timer < DataModule.GiftOpeningLength do
		wait(.1)
		GiftIcon.Rotation = math.random(-9,9)
	end
	
	local giftNumber = math.random(1,100)
	local giftFound = false
	local gift = nil
	while giftFound == false do
	for i, v in pairs(DataModule.Gifts) do
		if math.random(1,v) == giftNumber then
			giftFound = true
			gift = i
		end
	end
end

GiftIcon.Visible = false

local GiftImage = GiftImages:FindFirstChild(gift)
GiftImage.Visible = true
wait(2)
GiftImage.Visible = false

return gift
end


	clickDetector.MouseClick:Connect(function()
		if player.leaderstats.Coins.Value >= DataModule.GiftCost then
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - DataModule.GiftCost
	end
end)
	

		local giftOpened = ChooseGift(player)
		
		print(giftOpened)
		
		local gift = Gifts:FindFirstChild(giftOpened):Clone()
		ReplicatedStorage.Gifts.TeddyBloxpin.Parent = player.Backpack
		gift.Parent = game.Workspace
	end
end)

Not very organized, but the error is coming from PlayerAdded:Connect() not being closed. Can you clarify the purpose of this script?

1 Like

There are a few errors in your code regarding closing functions.
The first one is where you return gift, you need to close the first function there too:

return gift
end end)

But also the last function (MouseClick:Connect…
You have tried to close it twice

it kept saying player unknown so i just did that,

This function is outside the scope(environment) where the variable is. ClickDetectors actually return a value of the player who clicked.

Hello! I organized your script and found out the issue, You haved 2 ends at end of script
There is fixed version of your script:

local clickDetector = script.Parent

game.Players.PlayerAdded:Connect(function(player)

	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local DataModule = require(ReplicatedStorage:WaitForChild("DataModule"))
	local Gifts = ReplicatedStorage:WaitForChild("Gifts")

	local function ChooseGift(player)
		local PlayerGui = player:WaitForChild("PlayerGui")
		local MainGui = PlayerGui.MainGui
		local GiftIcon = MainGui.GiftIcons
		local GiftImages = MainGui.GiftImages

		local timer = tick()

		GiftIcon.Visible = true

		while tick() - timer < DataModule.GiftOpeningLength do
			wait(.1)
			GiftIcon.Rotation = math.random(-9,9)
		end

		local giftNumber = math.random(1,100)
		local giftFound = false
		local gift = nil
		while giftFound == false do
			for i, v in pairs(DataModule.Gifts) do
				if math.random(1,v) == giftNumber then
					giftFound = true
					gift = i
				end
			end
		end

		GiftIcon.Visible = false

		local GiftImage = GiftImages:FindFirstChild(gift)
		GiftImage.Visible = true
		wait(2)
		GiftImage.Visible = false

		return gift
	end


	clickDetector.MouseClick:Connect(function()
		if player.leaderstats.Coins.Value >= DataModule.GiftCost then
			player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - DataModule.GiftCost
		end
	end)


	local giftOpened = ChooseGift(player)

	print(giftOpened)

	local gift = Gifts:FindFirstChild(giftOpened):Clone()
	ReplicatedStorage.Gifts.TeddyBloxpin.Parent = player.Backpack
	gift.Parent = game.Workspace
end)

Also use ( tick() - timer) not tick() - timer this could lead to issue