-1

# Number of hits to leaderboards [closed]

Asked by 8 years ago

Hey! I'm having trouble coming up with a script to make it so when you hit X Block with X Item then a GUI will pop up and tell you how many more hits it will take to destroy it, then transporting that number to leaderboards.

Example: I found a rock and hit it with my pickaxe and then a GUI pops up to tell me how many more hits before its destroyed. Once destroyed the rock transfers to my leaderboards which shows how many rocks I have in total. Since I only destroyed one rock, under rock it would say 1.

How do I do this?

### Locked by FearMeIAmLag

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

1
jobro13 980
8 years ago

In these problems you have to deal with sub-problems (in most cases). The simplest way to solve these problems is to split them into these sub-problems, otherwise the task will be too complex. The more you script, the bigger "sub-problems" you can deal with.

Let's check it out. We basically have two main problems:

1) Showing a GUI how much hits we need 2) Showing the numbers of rock into the leaderboard

We can split these up in sub-problems:

1) a) Show a GUI with text on how many hits we need b) Transfer brick to inventory c) Find a way to store how much hits one has done on the block 2) a) Create leaderstats b) Update number

Problem 1a

The best way to program all these problems is to define variables to add functionality. As I don't know where you GUI is stored, I will provide "hooks", which will have to do a defined action (such as putting text on the GUI where you want it). You have to make sure these hooks work correctly.

function GetGui() -- a hook to get the guy itself
return game.ReplicatedStorage.HitsNeededGUI:Clone()
end

function ShowText(text)
local gui = GetGui()
gui.Text = text
gui.Parent = game.Players.LocalPlayer.PlayerGui
end


Problem 1b We only need to copy the rock to an inventory. UpdateLeaderstats is a hook for problem 2.

function TransferBlock(Block)
Block.Parent = game.Players.LocalPlayer.Inventory
end


Problem 1c The best way to store this - also when a player has resetted for example - is to store a value into the block itself. A problem is that 2 players can then "mine" a brick faster. I guess that we don't want this?

function GetIdentifier() -- a hook to return which value name we should store
return game.Players.LocalPlayer.Name
end

function BlockHit(Block, Tool) -- run this function after hitting Block with Tool
local ident = GetIdentifier()
if Block:FindFirstChild(ident) then
Block[ident].Value = Block[ident].Value + 1
else
local new = Instance.new("NumberValue", Block)
new.Name = ident
new.Value = 1
end
if Block[ident].Value >5 then
TransferBlock(block)
end

end



Problem 2a Initialize the leaderstats

local leaderstats = Instance.new("Model" , game.Players.LocalPlayer)
local rocks = Instance.new("NumberValue", leaderstats)
rocks.Name = "Rocks"


Problem 2b

We receive which we receive, and then update the leaderstats accordingly

function UpdateLeaderstats(Block)
if Block.Name == "Rock" then
rocks.Value = rocks.Value + 1
end
end


The important thing about all this is to think in hooks. If you create tiny functions which should do the expected thing you can upscale everything later. This is one of the main things I learned, after one of my biggest games became unhandlable due the fact I had scripted it without thinking in subproblems.