As you can see, the first time I do it, it runs once. But when I do it again it runs two times and so on.
The script is pretty long so I apologize
remotesFolder.Player.OpenCrate.OnClientEvent:Connect(function(crateName)
-- // Setup
pauseFrame.Shop.Visible = false
pauseFrame.Back.Visible = false
pauseFrame.CrateUnboxing.Visible = true
pauseFrame.BackgroundTransparency = 1
pauseFrame.CrateUnboxing.ItemViewport.ImageTransparency = 1
userInterfaceModule.hoverInfo(localPlayer, false)
currentCamera.CFrame = workspace.CrateOpening.OpeningCamera.CFrame
SoundService.UI.csgo_ui_crate_open:Play()
game.Lighting.ClockTime = 14.5
-- // Objects
local card = workspace.CrateOpening.Cards.Card
card.Position = Vector3.new(0, 92, 250)
card.Orientation = Vector3.new(0, 0, 0)
local leftCard = workspace.CrateOpening.Cards.LeftCard
leftCard.Position = Vector3.new(6, 92, 250)
leftCard.Orientation = Vector3.new(0, 0, 0)
local rightCard = workspace.CrateOpening.Cards.RightCard
rightCard.Position = Vector3.new(-6, 92, 250)
rightCard.Orientation = Vector3.new(0, 0, 0)
local doesOwn = workspace.CrateOpening.DoesOwn
doesOwn.Front.TextLabel.TextTransparency = 1
local frontGui = card.Front
local backGui = card.Back
local highlight = card.Highlight
frontGui.Enabled = false
highlight.FillTransparency = 1
highlight.OutlineTransparency = 1
local function setClickDetectors(num)
card.ClickDetector.MaxActivationDistance = num
leftCard.ClickDetector.MaxActivationDistance = num
rightCard.ClickDetector.MaxActivationDistance = num
end
local function hover(card, offset)
SoundService.UI.buttonrollover:Play()
card.ClickDetector.MouseHoverEnter:Connect(function()
TweenService:Create(card, TweenInfo.new(.1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(offset, 101, 250)}):Play()
end)
card.ClickDetector.MouseHoverLeave:Connect(function()
TweenService:Create(card, TweenInfo.new(.1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(offset, 100, 250)}):Play()
end)
end
TweenService:Create(card, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(0, 100, 250), Orientation = card.Orientation + Vector3.new(0, 180, 0)}):Play()
TweenService:Create(leftCard, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(6, 100, 250), Orientation = leftCard.Orientation + Vector3.new(0, 180, 0)}):Play()
TweenService:Create(rightCard, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(-6, 100, 250), Orientation = rightCard.Orientation + Vector3.new(0, 180, 0)}):Play()
task.wait(1) -- Wait for the tweens to finish, then enable the click detectors
setClickDetectors(math.huge)
if cratesModule.cratesList[crateName].Type == "Hats" then
local CRATE_CONTENTS = {}
for _, v in pairs(cratesModule.cratesList[crateName].Items) do
table.insert(CRATE_CONTENTS, {["Item"] = v, ["Weight"] = cratesModule.cratesList[crateName].Chances[ReplicatedStorage.AvatarItems.Hats[v]:GetAttribute("Rarity")]})
end
local UNBOXED_ITEM = getRandomItem(CRATE_CONTENTS)
frontGui.Container.ItemName.Text = ReplicatedStorage.AvatarItems.Hats[UNBOXED_ITEM]:GetAttribute("Name")
highlight.FillColor = raritiesModule.rarityList[ReplicatedStorage.AvatarItems.Hats[UNBOXED_ITEM]:GetAttribute("Rarity")].Color
highlight.OutlineColor = raritiesModule.rarityList[ReplicatedStorage.AvatarItems.Hats[UNBOXED_ITEM]:GetAttribute("Rarity")].Color
for _, v in ipairs(frontGui.Container.Outline:GetChildren()) do
v.BackgroundColor3 = raritiesModule.rarityList[ReplicatedStorage.AvatarItems.Hats[UNBOXED_ITEM]:GetAttribute("Rarity")].Color
end
for _, v in ipairs(frontGui.Container.InnerOutline:GetChildren()) do
v.BackgroundColor3 = raritiesModule.rarityList[ReplicatedStorage.AvatarItems.Hats[UNBOXED_ITEM]:GetAttribute("Rarity")].Color
end
if localPlayer.Inventory:FindFirstChild(UNBOXED_ITEM) then
remotesFolder.Change.ChangeMoney:FireServer("Add", cratesModule.cratesList[crateName].Price / 2, false)
doesOwn.Front.TextLabel.Text = "Sadly, you already own this item. You get 50% of your money back."
else
remotesFolder.Player.GiveItem:FireServer(UNBOXED_ITEM, "STAN_LOOSSEMBLE")
doesOwn.Front.TextLabel.Text = "New item unlocked!"
end
for _, v in ipairs(pauseFrame.CrateUnboxing.ItemViewport.WorldModel:GetChildren()) do
v:Destroy()
end
local itemModel = ReplicatedStorage.AvatarItems.Hats[UNBOXED_ITEM]:Clone()
itemModel.Parent = pauseFrame.CrateUnboxing.ItemViewport.WorldModel
end
local function openCard()
frontGui.Enabled = true
TweenService:Create(card, TweenInfo.new(.75, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Orientation = Vector3.new(0, 0, 0)}):Play() -- Tween to the front side of the card
TweenService:Create(highlight, TweenInfo.new(.75, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {FillTransparency = .5, OutlineTransparency = .5}):Play() -- Show the colored highlight
TweenService:Create(doesOwn.Front.TextLabel, TweenInfo.new(.75, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {TextTransparency = 0}):Play() -- Show the textlabel that tells the player if they already own it or not
SoundService.UI.csgo_ui_crate_display:Play()
task.wait(.75) -- Wait for the tweens to finish
TweenService:Create(pauseFrame.CrateUnboxing.ItemViewport, TweenInfo.new(.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {ImageTransparency = 0}):Play() -- Show the model of the hat
task.wait(5) -- After wait, return everything back to normal
pauseFrame.Shop.Visible = true
pauseFrame.Back.Visible = true
pauseFrame.CrateUnboxing.Visible = false
pauseFrame.BackgroundTransparency = .5
game.Lighting.ClockTime = remotesFolder.Command:InvokeServer("get_current_time")
for _, v in ipairs(workspace:GetChildren()) do
if v:GetAttribute("Map") then
currentCamera.CFrame = v.Camera.CFrame
end
end
end
hover(card, 0)
hover(leftCard, 6)
hover(rightCard, -6)
card.ClickDetector.MouseClick:Connect(function()
SoundService.UI.wpn_hudoff:Play()
setClickDetectors(0)
print("CLICKED")
task.wait(.1)
TweenService:Create(leftCard, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(6, 80, 250)}):Play() -- Hide the card that wasn't clicked
TweenService:Create(rightCard, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(-6, 80, 250)}):Play() -- Hide the card that wasn't clicked
task.wait(1)
openCard()
end)
leftCard.ClickDetector.MouseClick:Connect(function()
SoundService.UI.wpn_hudoff:Play()
setClickDetectors(0)
task.wait(.1)
TweenService:Create(card, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(0, 80, 250)}):Play() -- Hide the card that wasn't clicked
TweenService:Create(rightCard, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(-6, 80, 250)}):Play() -- Hide the card that wasn't clicked
task.wait(1)
TweenService:Create(leftCard, TweenInfo.new(.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(0, 100, 250)}):Play() -- Tween to the center
task.wait(.5)
TweenService:Create(card, TweenInfo.new(0), {Position = Vector3.new(0, 100, 250)}):Play() -- Quickly show the real card
TweenService:Create(leftCard, TweenInfo.new(0), {Position = Vector3.new(0, math.huge, 0)}):Play() -- Quickly hide the fake card
openCard()
end)
rightCard.ClickDetector.MouseClick:Connect(function()
SoundService.UI.wpn_hudoff:Play()
setClickDetectors(0)
task.wait(.1)
TweenService:Create(card, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(0, 80, 250)}):Play() -- Hide the card that wasn't clicked
TweenService:Create(leftCard, TweenInfo.new(1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(6, 80, 250)}):Play() -- Hide the card that wasn't clicked
task.wait(1)
TweenService:Create(rightCard, TweenInfo.new(.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Position = Vector3.new(0, 100, 250)}):Play() -- Tween to the center
task.wait(.5)
TweenService:Create(card, TweenInfo.new(0), {Position = Vector3.new(0, 100, 250)}):Play() -- Quickly show the real card
TweenService:Create(rightCard, TweenInfo.new(0), {Position = Vector3.new(0, math.huge, 0)}):Play() -- Quickly show the fake card
openCard()
end)
end)
I think the problem is with the functions inside the event. But I have no idea how to fix it