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

How do I acess the leaderboard from a different script?

Asked by 5 years ago

This is my server script for leaderboard in ServerScriptService If i wanted to access it how would I do it in another script for example if I killed a monster how would I get 100 USD for it?

datstore = game:GetService("DataStoreService")
local datast1 = datstore:GetDataStore("Yuan")
local datast2 = datstore:GetDataStore("USD")

game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder",plr)
    folder.Name = "leaderstats"
    local Yuan = Instance.new("IntValue",folder)
    Yuan.Name = "Yuan"
    local USD = Instance.new("IntValue",folder)
    USD.Name = "USD"
Yuan.Value = datast1:GetAsync(plr.UserId) or 0
datast1:SetAsync(plr.UserId, Yuan.Value)

USD.Value = datast2:GetAsync(plr.UserId) or 0
datast2:SetAsync(plr.UserId, USD.Value)

Yuan.Changed:Connect(function()
    datast1:SetAsync(plr.UserId, Yuan.Value)
end)
USD.Changed:Connect(function()
    datast2:SetAsync(plr.UserId, USD.Value)
end)

end)



0
Data stores are per place. User#19524 175 — 5y
0
What do you mean Data stores are per place isn't this a leaderboard? User#22788 5 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

What you're doing in lines 5 through 25 are setting up "IntValue"s. If you run your place in Solo mode, you can see these appear under game.Players.Halmuni.leaderstats. That's also the simplest way for other scripts to access those values. ex:

local players = game.Players:GetPlayers()
for i = 1, #players do
    local leaderstats = players[i]:FindFirstChild("leaderstats")
    if leaderstats then
        print("Player", players[i].Name, "has", leaderstats.USD.Value,"USD")
    end
end

In chat you also asked about ModuleScripts. They are an excellent way to share information and functionality between scripts, but they don't automatically transfer data between server and client - you would have to script that yourself.

To illustrate, let's take a very simple module script; let's say it's at game.ReplicatedStorage.TestModule

local module = {}
local var = 0
function module.IncreaseValue()
    var = var + 1
end
function module.GetValue()
    return var
end
return module

Here's a sample server script:

local TestModule = require(game.ReplicatedStorage.TestModule)
TestModule.IncreaseValue()
print(TestModule.GetValue()) -- 1

Here's another server script:

local TestModule = require(game.ReplicatedStorage.TestModule)
wait() -- ensure that the first server script runs first
print(TestModule.GetValue()) -- also prints 1 because the state is shared

However, if a client script also existed that was identical to the last server script, it would print out '0' because the state (ie the variable 'var' and any other variables in the ModuleScript) is not shared between server and client.

0
if the client should not touch the value, then keep it in the server script service or server storage User#19524 175 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago

You would use this line of code:

game.Players.LocalPlayer.leaderstats.USD = game.Players.LocalPlayer.leaderstats.USD + 100

Answer this question