Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can I get some help with RemoteEvents and DataStore?

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

0

You are not even calling the function so the script does not know what "plr" means.

0

I'm calling the function inside my save script. Let me update post

0

Updated post

1 answer

✓ Accepted answer

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.

4 comments

0

I changed it to plr.leaderstats.Coins.Value but it still doesn't work :( It returns the output as the post

0

In the DataStore script, instead of Data:FireClient(plr) inside of the PlayerAdded event change it to SaveData(plr)

0

What you said worked but it only worked in studio. Please help me!

0

Ok nvm sorry!

This discussion is preserved as it was. New answers, comments, and votes are closed.