Hello Devs!
Im still quite bad at scripting, and I need some help with a heart system. I already have a draft, and it doesnt work too well. If the player loses hearts slowly, it does what its meant to do, but if the player loses hearts quickly, because of the 1 second cooldown, the script breaks. Ive tried lowering the cooldown but that breaks the animation.
! This script is really messy and is probably done in an inefficient way !
local TweenService = game:GetService("TweenService")
local Heart1 = script.Parent.Heart1
local Heart2 = script.Parent.Heart2
local Heart3 = script.Parent.Heart3
local BrokenID = "rbxassetid://15738277774"
local FixedID = "rbxassetid://15738280507"
local function createBrokenHeart(originalHeart)
local newHeart = originalHeart:Clone()
newHeart.Image = BrokenID
newHeart.Parent = originalHeart.Parent
newHeart.Position = originalHeart.Position
return newHeart
end
local function animateHeartLoss(originalHeart)
if originalHeart.Image == BrokenID then
return
end
local brokenHeart = createBrokenHeart(originalHeart)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quart, Enum.EasingDirection.Out)
local endPosition = originalHeart.Position + UDim2.new(0, 0, 0, 50)
local tween = TweenService:Create(originalHeart, tweenInfo, {
Position = endPosition,
ImageTransparency = 1,
})
tween:Play()
tween.Completed:Connect(function()
originalHeart:Destroy()
brokenHeart.Visible = true
end)
end
local function resetHearts()
script.Parent.Heart1.Image = FixedID
script.Parent.Heart2.Image = FixedID
script.Parent.Heart3.Image = FixedID
end
local player = game.Players.LocalPlayer
local Values = player:WaitForChild("Values")
local Hearts = Values:WaitForChild("Hearts")
-- I think the error starts from around here
while true do
wait(1)
local HeartsValue = Hearts.Value
if HeartsValue == 3 then
resetHearts()
elseif HeartsValue == 2 then
animateHeartLoss(script.Parent.Heart1)
Heart2.Image = FixedID
Heart3.Image = FixedID
elseif HeartsValue == 1 then
animateHeartLoss(script.Parent.Heart2)
Heart3.Image = FixedID
elseif HeartsValue == 0 then
animateHeartLoss(script.Parent.Heart3)
end
end
I thought the error was that I need to make it check if the hearts value is 2 or less, 1 or less etc using <=, but that introduces more problems.
Apologies for having such little knowledge about this topic
Sure, robloxapp-20231230-1241481.wmv (458.5 KB)
Sorry for the rough UI, havent got to designing it yet
(if I do it faster the hearts dont go at all, or just one heart goes like the middle one)
function CheckTopping(button, player)
if not currentTopping then
SetRandomTopping()
end
if button.Name == currentTopping then
print("Correct!")
else
game.Players.LocalPlayer.Values.Hearts.Value = math.clamp(game.Players.LocalPlayer.Values.Hearts.Value - 1, 0, 3)
print("Wrong!")
end
SetRandomTopping()
end
Yes, it doesnt go above or below now, but I still have the error.
If I Iose hearts very slowly, the script works, heres what it does: robloxapp-20231230-1302544.wmv (146.0 KB)
But the problem is that the player will lose hearts very quickly, causing the heart system to break as shown in the other video
i have an idea, but it will require some time, hide the buttons using
function hideAndShowButton(ButtonOfToppingIdkTheName)
ButtonOfToppingIdkTheName.Visible = false
wait(1)
ButtonOfToppingIdkTheName.Visible = true
end
--in the wrong topping for the if ... then
for i, button in pairs(buttons:GetChildren()) do
coroutine.wrap(hideAndShowButton)(button) -- coroutine is going to countinue the for i loop without doing it one after the other
end