Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Number of hits to leaderboards [closed]

Asked by 11 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.

Why was this question closed?

2 answers

Log in to vote
1
Answered by
jobro13 980 Moderation Voter
11 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.

1function GetGui() -- a hook to get the guy itself
2    return game.ReplicatedStorage.HitsNeededGUI:Clone()
3end
4 
5function ShowText(text)
6local gui = GetGui()
7gui.Text = text
8gui.Parent = game.Players.LocalPlayer.PlayerGui
9end

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

1function TransferBlock(Block)
2Block.Parent = game.Players.LocalPlayer.Inventory
3UpdateLeaderstats(Block)
4end

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?

01function GetIdentifier() -- a hook to return which value name we should store
02return game.Players.LocalPlayer.Name
03end
04 
05function BlockHit(Block, Tool) -- run this function after hitting Block with Tool
06local ident = GetIdentifier()
07if Block:FindFirstChild(ident) then
08    Block[ident].Value = Block[ident].Value + 1
09else
10    local new = Instance.new("NumberValue", Block)
11    new.Name = ident
12    new.Value = 1
13end
14if Block[ident].Value >5 then
15    TransferBlock(block)
16end
17 
18end

Problem 2a Initialize the leaderstats

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

Problem 2b

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

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

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.

Ad
Log in to vote
-2
Answered by
Bubby4j 231 Moderation Voter
11 years ago

It depends how you store damage to a rock. You could have a NumberValue in the rock, add to it whenever someone hits it. Whenever it reaches another NumberValue in the rock which contains the Max Damage the rock can take, it breaks.

To display this, you could just make a frame with a TextLabel which shows (MaxDamage - Damage)/Damage Caused by tool. That would be the # of hits required to make the rock fall apart.

You could update it when the .Target of the mouse changes, and whenever the Damage NumberValue of the Target changes, so you could see any rock, even before you hit it.

Adding to the leaderboard is simple, see Roblox Wiki/Leaderboards.