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

Drop cash on death math.random and billboardgui don't match up with each others values?

Asked by 8 years ago

Ok so what this does is drops a part named credit that players can pick up and I will provide a picture of the hierarchy (Explorer) and will also provide the script it uses math.random(10,100) to drop the cash but the billboardgui under the part don't show the correct amount that is in the credit part.

wait(0.1)
local scr = script.Parent
local amount = math.random(10,100)
script.Parent.BillboardGui.TextLabel.Text = amount

if scr.Parent.Name == "Backpack" then


    local player = scr.Parent.Parent
    while player.Character == nil do   
    print ("NO CHARACTER")
    wait(1)
    end
    print (player)

    function onDied()

    if player.Money.Value >= amount then
    player.Money.Value = player.Money.Value - amount
    local j = scr:Clone()
    j.Position = player.Character.Torso.Position
    j.Parent = game.Workspace
    print("Money Dropped")
    end

    end
    player.Character.Humanoid.Died:connect(onDied)


else
    wait(1)
    local enabled = true

    function onTouch(part)

    local p = game.Players:playerFromCharacter(part.Parent)  
    if p == nil then return end

    if not enabled then return end
    enabled = false

    print("Money")
    p.Money.Value = p.Money.Value + amount 
    wait()
    scr:Remove()

    end
    scr.Touched:connect(onTouch)

end

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago
wait(0.1)
local scr = script.Parent
if scr.Parent.Name == "Backpack" then
    local amount = math.random(10,100)
    local player = scr.Parent.Parent
        while player.Character == nil do   
            print ("NO CHARACTER")
            wait(1)
        end 
    print (player)
        function onDied()
            if player.Money.Value >= amount then
                player.Money.Value = player.Money.Value - amount
                script.Parent.BillboardGui.TextLabel.Text = amount
                local j = scr:Clone()
             j.Position = player.Character.Torso.Position
             j.Parent = game.Workspace
            print("Money Dropped")
        end
    end
player.Character.Humanoid.Died:connect(onDied)
else
wait(1)
local enabled = true
    function onTouch(part)
        local p = game.Players:playerFromCharacter(part.Parent)  
            if p == nil then return end
            if not enabled then return end
            enabled = false
            print("Money")
            p.Money.Value = p.Money.Value + amount 
            wait()
            scr:Remove()
        end
    scr.Touched:connect(onTouch)
end

I put both the if statement and the script.Parent.BillboardGui.TextLabel.Text = amount into the if statement. I've dealt with this kind of stuff before, and I remember it calling different numbers each function/if statement. Hopefully this works. I don't really have an explanation for why I did what I did, because in my mind, it works out perfectly, but I can't really put it into words.

Ad

Answer this question