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

Value wont change?

Asked by 9 years ago

Well I posted something not too long ago about a GUI in my game not changing to the Value of a NumberValue. Now that's all fixed thanks to people here :) another problem has come up, I want that value to be changed every second a player is standing on a certain brick. This is a localscript in the brick:

1script.Parent.Touched:connect(function(player)
2    local persongui = player.PlayerGui.MoneyGui.TextLabel.Money
3    while true do
4        wait(1)
5            persongui.Value = persongui.Value + 10
6    end
7end)

I need help, no Output errors it just wont work. Any help is appreciated :)

1 answer

Log in to vote
17
Answered by 9 years ago

Your fix is easy.

don't worry, it was a simple mistake.

I'll quickly run you through this.

First, It would be easier to replace the function's variable player, and simply call it hit.

Why? Because when you touch a part, The player value in your sense is the PART in your player's character, not the player.

so replace line 1 with

1script.Parent.Touched:connect(function(hit)

And put a line between lines 1 and 2:

1player=game.Players:GetPlayerFromCharacter(hit.Parent)

then add an if loop to see if it's not just some part.

here's the finished code.

01script.Parent.Touched:connect(function(hit)
02player=game.Players:GetPlayerFromCharacter(hit.Parent)
03 
04if player then
05    local persongui = player.PlayerGui.MoneyGui.TextLabel.Money
06    while true do
07        wait(1)
08            persongui.Value = persongui.Value + 10
09    end
10end
11end)

That's All you really need. But judging you'll hit this part 2-5 times, you may want to put in a debounce.

Here's that.

01debounce=false
02 
03script.Parent.Touched:connect(function(hit)
04player=game.Players:GetPlayerFromCharacter(hit.Parent)
05 
06if player and not debounce then
07debounce=true
08    local persongui = player.PlayerGui.MoneyGui.TextLabel.Money
09    while true do
10        wait(1)
11            persongui.Value = persongui.Value + 10
12    end
13debounce=false
14end
15end)

BUT!

This debounce has no effect except only being able to be touched once. How to fix?

Easy.

Simply use a repeat instead of a while loop. This will give your 'money giver' a limited amount of time.

01debounce=false
02waitTime=10
03 
04script.Parent.Touched:connect(function(hit)
05player=game.Players:GetPlayerFromCharacter(hit.Parent)
06 
07if player and not debounce then
08debounce=true
09    local persongui = player.PlayerGui.MoneyGui.TextLabel.Money
10time=10
11 repeat
12        wait(1)
13            persongui.Value = persongui.Value + 10
14    until time>=waitTime
15 
16debounce=false
17end
18end)

For example, above, this code will become untouchable, give the specified player 10C/second until the time variable = the waitTime variable. Then debounce turns off.

Here's some nice wiki pages on your subject.

http://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayerFromCharacter ~GetPlayerFromCharacter

http://wiki.roblox.com/index.php?title=Debounce ~Debounce http://wiki.roblox.com/index.php?title=Loops ~Different kinds of loops

That's all I have for today. If you liked the answer, slap that upvote and accept this answer IN THE FACE!

baibai

EEDIT: None of this worked lol

2
Again, no output errors. Just no Value changing either. I double checked to make sure I have to position right, like where the Value is. It really looks like it should work idk why it isn't D: lol CrispyBrix 113 — 9y
Ad

Answer this question