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

Can I get some help with RemoteEvents and DataStore?

Asked by 7 years ago
Edited 7 years ago

I'm new to RemoteEvents and FilteringEnabled so I need some help!

Output:

14:30:43.789 - ServerScriptService.RemoteScripts.DataStore:8: attempt to index local 'plr' (a nil value)
14:30:43.790 - Stack Begin
14:30:43.790 - Script 'ServerScriptService.RemoteScripts.DataStore', Line 8
14:30:43.790 - Stack End
14:30:43.955 - leaderstats is not a valid member of Player
14:30:43.956 - Stack Begin
14:30:43.956 - Script 'Players.Player1.PlayerGui.SurfaceGui.Frame.Level.LocalScript', Line 1
14:30:43.956 - Stack End
14:30:43.957 - leaderstats is not a valid member of Player
14:30:43.957 - Stack Begin
14:30:43.957 - Script 'Players.Player1.PlayerGui.SurfaceGui.Frame.Coins.LocalScript', Line 1
14:30:43.958 - Stack End

DataStore script:

local DS = game:GetService("DataStoreService"):GetDataStore("Data")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Data = ReplicatedStorage.Remotes:WaitForChild("Data")
local DataLeave = ReplicatedStorage.Remotes:WaitForChild("DataOnLeaving")

function SaveData(plr)

    local key = "UserId-"..plr.UserId
    local leaderstats = Instance.new("IntValue", plr)
    local Coins = Instance.new("IntValue")
    local Level = Instance.new("IntValue")
    leaderstats.Name = "leaderstats"
    Coins.Name = "Coins"
    Coins.Parent = leaderstats
    Level.Name = "Level"
    Level.Parent = leaderstats

    local GetSaved = DS:GetAsync(key)
     if GetSaved then
        Coins.Value = GetSaved[1]
        Level.Value = GetSaved[2]
    else
        local ToSave = {Coins.Value, Level.Value}
        DS:SetAsync(key, ToSave)
    end

end

function SaveWhenLeaving(plr)
        local key = "UserId-"..plr.UserId
        local SaveTable = {plr.leaderstats.Coins.Value, plr.leaderstats.Level.Value}
        DS:SetAsync(key, SaveTable)
end


Data.OnClientEvent:Connect(SaveData)
DataLeave.OnClientEvent:Connect(SaveWhenLeaving)

game.Players.PlayerAdded:Connect(function(plr)
    Data:FireClient(plr)
end)

game:BindToClose(function()
    game.Players.PlayerRemoving:Connect(function(plr)
        DataLeave:FireClient(plr)
    end)
end)

Coins script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CoinGiver = ReplicatedStorage.Remotes:WaitForChild("GiveCoins")

function GiveCoin(plr, num)
    local CoinValue = game.Players[plr].leaderstats.Coins.Value
    return CoinValue + num
end

CoinGiver.OnClientEvent:Connect(GiveCoin)

The level script is the same as the coins script but it uses a different remote

0
You are not even calling the function so the script does not know what "plr" means. FiredDusk 1466 — 7y
0
I'm calling the function inside my save script. Let me update post User#16921 0 — 7y
0
Updated post User#16921 0 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

In your Coins Script, you have game.Players[plr].leaderstats.Coins.Value, but since plr is already a Player instance, there is no need for the game.Players part of that line. Go ahead and remove it and tell me if it works.

0
I changed it to plr.leaderstats.Coins.Value but it still doesn't work :( It returns the output as the post User#16921 0 — 7y
0
In the DataStore script, instead of Data:FireClient(plr) inside of the PlayerAdded event change it to SaveData(plr) AlreadyPro 153 — 7y
0
What you said worked but it only worked in studio. Please help me! User#16921 0 — 7y
0
Ok nvm sorry! User#16921 0 — 7y
Ad

Answer this question