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

How can I use "player" in a connection line of a regular script?

Asked by 9 years ago

I'm trying to work on scripting a save stats feature, and I want the players' stats to save when their stats change. The only problem is that I don't know how to use "Player" when in a connection line of a regular script, not Local Script. Here's the save script, if you need the code:

function saveStats(Player)
    repeat wait() until Player
    --gold and gems save
    Player:SaveNumber(goldKey, Player.leaderstats.Gold.Value)
    Player:SaveNumber(gemsKey, Player.leaderstats.Gems.Value)
    --level and xp save
    Player:SaveNumber(levelKey, Player.leaderstats.Level.Value)
    Player:SaveNumber(xpKey, Player.Exp.Value)
end

How would a connection line work? Also, if you can find a way to make this in a LocalScript feel free, just nothing has been working for me in LocalScripts.

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

This all depends on what triggers this.


Is this just something running constantly? Every 10 seconds or so? In that case, you can just loop over players yourself and call the function:

while wait(10) do
    for _, player in pairs(game.Players:GetPlayers()) do
        saveStats(player);
    end
end

To do it when they leave, you can just use PlayerRemoving event:

game.Players.PlayerRemoving:connect( saveStats );

Or a mixture of the two. Or other triggers, like from a Touch event on a part:

function saveOnTouch(hit)
    if hit.Parent then
        local character = hit.Parent;
        local player = game.Players:GetPlayerFromCharacter(character);
        if player then
            saveStats(player);
        end
    end
end
somePart.Touched:connect( saveOnTouch );

To save on change, you would use an anonymous function in what is called a closure. This is also how Chatted events are usually done.

someValue.Changed:connect(function() saveStats(someValue.Parent) end);

(Where someValue is a child of a player that you are tracking changes on to save stats)


Please note that you should remove the repeat loop from your function. Since it's a repeat loop, and not a while loop, it will always wait at least once (which is bad) and since player is a parameter, it can't actually change so the line doesn't accomplish anything.

1
I want it so that when the stats are changed, it runs the function, like the example I made. I'm just not sure how to connect it to the player. SlickPwner 534 — 9y
0
Sorry, I missed that -- my post now addresses that BlueTaslem 18071 — 9y
Ad
Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Since BlueTaslem didn't address your comment, I'll add this answer.

As Data Persistence is not exactly deprecated, I highly suggest learning to use DataStores. They give two main benefits over Data Persistence: you can save whatever you want, as long as it isn't pure ROBLOX Objects (i.e. not serialized.), and you can access the saved data whenever you want, unlike DP which requires the Player be there.

Anyway, since you are creating the Value objects you are saving the Values of somewhere, I suggest using the Changed event on the Value objects, and saving the data for that specific Value object there.

Here's an example of that:

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

    local gold = Instance.new("IntValue", stats)
    gold.Name = "Gold"
    gold.Changed:connect(function()
        Player:SaveNumber(goldKey, gold.Value)
    end)
    --And so on for the other three stats.
end)

Answer this question