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

Value wont change?

Asked by 8 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:

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

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

1 answer

Log in to vote
17
Answered by 8 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

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

And put a line between lines 1 and 2:

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

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

here's the finished code.

script.Parent.Touched:connect(function(hit)
player=game.Players:GetPlayerFromCharacter(hit.Parent)

if player then
    local persongui = player.PlayerGui.MoneyGui.TextLabel.Money
    while true do
        wait(1)
            persongui.Value = persongui.Value + 10
    end
end
end)


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.

debounce=false

script.Parent.Touched:connect(function(hit)
player=game.Players:GetPlayerFromCharacter(hit.Parent)

if player and not debounce then
debounce=true
    local persongui = player.PlayerGui.MoneyGui.TextLabel.Money
    while true do
        wait(1)
            persongui.Value = persongui.Value + 10
    end
debounce=false
end
end)


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.

debounce=false
waitTime=10

script.Parent.Touched:connect(function(hit)
player=game.Players:GetPlayerFromCharacter(hit.Parent)

if player and not debounce then
debounce=true
    local persongui = player.PlayerGui.MoneyGui.TextLabel.Money
time=10
 repeat
        wait(1)
            persongui.Value = persongui.Value + 10
    until time>=waitTime

debounce=false
end
end)


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 — 8y
Ad

Answer this question