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

3 comments
You are not even calling the function so the script does not know what "plr" means.
I'm calling the function inside my save script. Let me update post
Updated post