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

I need help on making an while number value adding loop?

Asked by 8 years ago

When the player touches the ball, it creates Number Value. What I want it to do is when the player touches ball, the number value will keep on growing to a bigger number. I also what it to stop in 2 ways. 1 if it reaches to 200, it stops counting. 2 when somebody else touches the ball, it stops counting the person number who previously touched it. I try to do the number the loop but I did not under stand what i did.

local BallTarget = game.Workspace.BallTarget
local PlayersName = game.Workspace.BallTarget.BillboardGui.PlayersName

BallTarget.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and
game.Players:GetPlayerFromCharacter(hit.Parent) then
    PlayersName.Text = hit.Parent.Name
    BallTarget.Material = ("Neon")
    BallTarget.BrickColor = hit.Parent:FindFirstChild("Torso").BrickColor
        numvalue.Value = 0
        while true do
            number = numvalue.Value + 1
            print(number)
            wait(1)
        end
    end
end)

function BallTargethit(otherPart)
    if otherPart ~= nil and otherPart.Parent ~= nil and otherPart.Parent:FindFirstChild("Humanoid") then
        print("A player hit me!")
        if not BallTarget:FindFirstChild(otherPart.Parent.Name) then    
            local playerTag = Instance.new("NumberValue")
            playerTag.Parent = BallTarget
            playerTag.Name = otherPart.Parent.Name
        end
    end
end
    numvalue.Value = 0
    while true do
        number = numvalue.Value + 1
        Value = (number)
        wait(1)
    end

BallTarget.Touched:connect(BallTargethit)
0
Well to start, what you would want to do with the loop is make it it's own script and have it cloned to the NumberValue. Next thing you can do is while numvalue.Value > 200 do, this basically means that while the Value is under 200 it will continue going. OneTruePain 191 — 8y

1 answer

Log in to vote
2
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

Organizing Data

The easiest way to accomplish what you want isn't by using NumberValues, but rather by using a table (or more specifically a dictionary). This way you can assign a Key to the table for each player who touches the ball that holds a value equal to the number of seconds they have been touching it. All we need to do is check if the player's points have been set yet, and initialize them to zero if not:

local points = {}

Ball.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and not points[player] then
        points[player] = 0
    end
end)

Adding Points

Now we'll need a loop to add points to the last person who touched the ball's score. We'll create a new variable called LastPlayerTouched that we will have update each time a new player touches the ball. Then we'll call a new function called AddPoints that takes one argument, the player. This function creates a while loop that will run until LastPlayerTouched is no longer equal to the argument player (i.e. when another player touches the ball):

local LastPlayerTouched = nil

local function AddPoints(player)
    while LastPlayerTouched == player do
        points[player] = points[player] + 1
        wait(1)
    end
end

Updating the Ball

Now all we need to is write a function that changes the Ball and BillboardGui to reflect the player who is currently earning points:

local PlayersName = game.Workspace.BallTarget.BillboardGui.PlayersName

local function UpdateBall(player)
    PlayersName.Text = player.Name
    BallTarget.Material = ("Neon")
    BallTarget.BrickColor = hit.Parent:FindFirstChild("Torso").BrickColor
end

Putting it all Together

local PlayersName = game.Workspace.BallTarget.BillboardGui.PlayersName
local LastPlayerTouched = nil
local score = {}

local function AddPoints(player)
    while LastPlayerTouched == player do
        points[player] = points[player] + 1
        wait(1)
    end
end

local function UpdateBall(player)
    PlayersName.Text = player.Name
    BallTarget.Material = ("Neon")
    BallTarget.BrickColor = hit.Parent:FindFirstChild("Torso").BrickColor
end

Ball.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and not points[player] then
        points[player] = 0
    end
    if player and LastPlayerTouched ~= player then
        LastPlayerTouched  == player
        AddPoints(player)
        UpdateBall(player)
    end
end)
Ad

Answer this question