I have my scripts already finished to have a leaderboard that generates a random number that will be the amount of lives someone has in my game and then when they die in the game (from falling out of the map or sword fighting etc) they lose 1 life. However, I noticed a bug in the feature that I don’t want to be part of the script because I want something to happen when the player reaches 0 lives so it can’t go past 0 lives and into -1 lives or further.
I know how to use if statements in most languages but forgot how to use them in Roblox Lua and I tried making the if statement myself so that if the counter reaches 0 it doesn’t subtract any more lives but I write messy code so I wanted some help to know what good code looks like for this script.
I looked for other posts on Devforum but my problem is unique to the game I’m trying to create.
Here is the script that makes the leaderboard and gives the player a random amount of lives
game.Players.PlayerAdded:Connect(function(players)
local leaderboard = Instance.new("BoolValue", players)
leaderboard.Name = "leaderstats"
local Lives = Instance.new("IntValue", leaderboard)
Lives.Name = "Lives"
Lives.Value = math.random(1,2)
print(Lives.Value)
players.CharacterAdded:Connect(function(character)
end)
end)
Here is the script that deducts a life from the leaderboard life counter
(I need help changing this script so that it adds an if statement that basically tells the server "If player has 0 lives then they can no longer lose lives, maybe with an else statement somewhere to explain this if its better than using an if statement)
local char = script.Parent
local Human = char:WaitForChild("Humanoid")
local Players = game:GetService("Players")
local Player = Players:GetPlayerFromCharacter(char)
Human.Died:Connect(function()
Player.leaderstats.Lives.Value -= 1
end)
Thanks for reading.