1. What do I want to achieve?:
I am making a function that increases a player’s stat multiplier by clicking a button, you’ll see that I didn’t know how to make this more optimized so I made it like this:
local function IncreaseMultiplier(stat: "Strength" | "Agility" | "Power")
print(stat)
confirm.Visible = true
frame.Visible = false
confirm:TweenSize(UDim2.new(0.25,0,0.25,0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 1)
confirm.No.Activated:Connect(function()
confirm.Visible = false
confirm:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 0)
frame.Visible = true
end)
confirm.Yes.Activated:Connect(function()
confirm.Visible = false
confirm:TweenSize(UDim2.new(0,0,0,0), Enum.EasingDirection.Out, Enum.EasingStyle.Elastic, 0)
frame.Visible = true
if stat == "Strength" then
print(stat)
purchaseMlt:FireServer("SthMultiplier")
elseif stat == "Agility" then
print(stat)
purchaseMlt:FireServer("AgyMultiplier")
elseif stat == "Power" then
print(stat)
purchaseMlt:FireServer("PwrMultiplier")
end
end)
end
statsFrame.Strength.Multiplier.Activated:Connect(function()
IncreaseMultiplier("Strength")
end)
statsFrame.Agility.Multiplier.Activated:Connect(function()
IncreaseMultiplier("Agility")
end)
statsFrame.Power.Multiplier.Activated:Connect(function()
IncreaseMultiplier("Power")
end)
In ModuleScript:
function manager.GetMultiplier(player: Player, multiplier: string)
local profile = manager.Profiles[player]
if not profile then return end
profile.Data[multiplier] *= 2
player.Multipliers[multiplier].Value = profile.Data[multiplier]
updateMultiplier:FireClient(player)
end
purchaseEvent.OnServerEvent:Connect(manager.GetMultiplier)
2. What is the issue?
As you are seeing, I added prints to know what’s going on with the script and in the statements part, they are supposed to read the stat string and use that string to FireServer the purchase event and then the manager.GetMultiplier gets care of reading and finding that string in player’s data and double the multiplier.
However, the issue is that whenever I cancel the confirmation by clicking confirm.No button and then when I use it for other stat instead, the string of earlier somehow saves and I end multiplying two stats.
3. What solutions have I tried so far?
I tried to add task.spawn for the function to run once, didn’t work. I was going to try corountine.yield but from what I read, it doesn’t seem to help. I don’t recognize what is causing this mistake so is there somebody that can explain what am I doing wrong? Thanks