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

Help with PlayerAdded and Changed() events?

Asked by 9 years ago
game.Players.PlayerAdded:connect(function(player)
    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local level = Instance.new("IntValue", stats)
    level.Name = "Level"

    local exp = Instance.new("IntValue", stats)
    exp.Name = "Exp"

    local cash = Instance.new("IntValue", player)
    cash.Name = "Cash"

    local class = Instance.new("StringValue", player)
    class.Name = "Class"
    class.Value = "Laser"

    local team = Instance.new("StringValue", player)
    team.Name = "Team"
    team.Value = "White"

    player:WaitForDataReady()
    player.leaderstats.Level.Value = player:LoadNumber("Level")
    player.leaderstats.Exp.Value = player:LoadNumber("Exp")
    player.Cash.Value = player:LoadNumber("Cash")

    level.Changed:connect(function(level)
        print("Levelup")
        levelupframe:TweenPosition(UDim2.new(0.5, -150, 0.5, -50), "Out", "Quad", 1, false)
        leveluptext.Text =  "You have leveled up to Level: " .. level
        levelupframe.CashText.Text = "Cash + 50"
        wait("3")
        levelupframe:TweenPosition(UDim2.new(0, -300, 0.5, -50), "Out", "Quad", 1, false)
    end)
end)

There is nothing printing in the output

0
Try to do it without the WaitForChild's. If that doesn't work then make .changed another function bellow and remove it from top. Can't say i'll be right alphawolvess 1784 — 9y
0
still dosent work :/ NinjoOnline 1146 — 9y

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

I'm not sure where the first code block fits in, and the two functions in the second code block are better written as a single function.

The problem, as I think it is, is that the leaderstats get created, and then you Change them to load the Player's level, causing the leveled up message to appear. To get around that, you have to connect the Changed event after you load the player's data:

game.Players.PlayerAdded:connect(function(player)
    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local level = Instance.new("IntValue", stats)
    level.Name = "Level"

    player:WaitForDataReady()
    player.leaderstats.Level.Value = newPlayer:LoadNumber("Level")

    level.Changed:connect(function(level)
        levelupframe:TweenPosition(UDim2.new(0.5, -150, 0.5, -50), "Out", "Quad", 1, false)
        leveluptext.Text =  "You have leveled up to Level: " .. level
        levelupframe.CashText.Text = "Cash + 50"
        wait("3")
        levelupframe:TweenPosition(UDim2.new(0, -300, 0.5, -50), "Out", "Quad", 1, false)
    end)

end)

If you have those two lines in separate scripts, use a BoolValue to tell the client when to connect the event:

game.Players.PlayerAdded:connect(function(player)
    local stats = Instance.new("IntValue", player)
    stats.Name = "leaderstats"

    local level = Instance.new("IntValue", stats)
    level.Name = "Level"

    local bool = Instance.new("BoolValue", player)
    bool.Name = "Bool"

    player:WaitForDataReady()
    player.leaderstats.Level.Value = newPlayer:LoadNumber("Level")

    bool.Value = true
end)
    local bool = player:WaitForChild("Bool")
    while bool.Value ~= true do
        bool.Changed:wait()
    end

    player.leaderstats.Level.Changed:connect(function(level)
        levelupframe:TweenPosition(UDim2.new(0.5, -150, 0.5, -50), "Out", "Quad", 1, false)
        leveluptext.Text =  "You have leveled up to Level: " .. level
        levelupframe.CashText.Text = "Cash + 50"
        wait("3")
        levelupframe:TweenPosition(UDim2.new(0, -300, 0.5, -50), "Out", "Quad", 1, false)
    end)
0
I eddited my answer with what you have, and it dosent create the exp and other values. Please look and help NinjoOnline 1146 — 9y
0
I editted again, now it creates the exp and other values, and I added a print so I can check if the levelup thingy has work, but when you level up it dosent print NinjoOnline 1146 — 9y
Ad

Answer this question