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

Having trouble with replicated storage?

Hey guys, another question.

I've added Exp to my game and while the ranks of players save, Exp does not. This means that if someone ranks up in my game, they will eventually lose everything because ranks are based on Exp and they would revert back to rank 1 due to having 0 Exp.

With it already working for the ranks, how would I implement Exp to also be saved? I've put a "-- Here" indicated where I've attempted to do this.

local DS = game:GetService('DataStoreService'):GetDataStore('Data')
local event = game.ReplicatedStorage.Event;


function onPlayerEntered(plr)
	repeat wait() until plr.Character
    local stats = Instance.new("Folder")
	stats.Parent = plr
    stats.Name = "leaderstats"

    local rank = Instance.new("StringValue")
	rank.Parent = stats
    rank.Name = "Rank" 
	rank.Value = "Newbie"
	
	local val = Instance.new("IntValue")
	val.Parent = stats
	val.Name = "RankVal"
	val.Value = 1
	
	local exp1 = Instance.new("IntValue")
	exp1.Parent = stats
	exp1.Name = "Exp"
	exp1.Value = 0

    local Key = plr.UserId
    if DS:GetAsync(Key) == nil or DS:GetAsync(Key) == 0 then
        DS:SetAsync(Key, 1)
    end
    plr.leaderstats.RankVal.Value = DS:GetAsync(Key)
    plr.leaderstats.Exp.Value = DS:GetAsync(Key) -- Here
	print("fired")
	event:FireAllClients(plr.Name, 1);
	
	-- attach the leaderstats changed event here
	val.Changed:connect(function(value)
		print("check'em");
		event:FireAllClients(plr.Name, value);
	end);
end

game.Players.PlayerAdded:connect(onPlayerEntered)

game.Players.PlayerRemoving:connect(function(plr)
    local stats = plr.leaderstats
    local Key = plr.UserID
    DS:SetAsync(Key, stats.RankVal.Value)
	DS:SetAsync(Key, stats.Exp.Value) -- Here
end)

1 answer

In that case, using a table really helps! Here's the script for my suggestion: (No Replicated Storage needed!)

local dss = game:GetService("DataStoreService"):GetDataStore("Data") -- Give it a name.
--To be honest, you don't need ReplicatedStorage to do this...

game.Players.PlayerAdded:connect(function(plr)
	--Variables
		--DataStore (Plan carefully! Mistakes may lead to lots of Data Store problems!)
	local UniqueKey = 'id-'..plr.UserId
	
		--Leaderstats (Optional)
		--Give each variable a different name!
local stats = Instance.new("Folder")
stats.Parent = plr
stats.Name = "leaderstats"

local rank = Instance.new("StringValue")
rank.Parent = stats
rank.Name = "Rank" 
rank.Value = "Newbie"

local val = Instance.new("IntValue")
val.Parent = stats
val.Name = "RankVal"
val.Value = 1

local exp1 = Instance.new("IntValue")
exp1.Parent = stats
exp1.Name = "Exp"
exp1.Value = 0
		
	--GetAsync (Time for the real thing!)
local GetSaved = dss:GetAsync(UniqueKey)
if GetSaved then
	rank.Value = GetSaved[1]
	val.Value = GetSaved[2]
	exp1.Value = GetSaved[3]
else
	local toSave = {rank.Value, val.Value, exp1.Value}
	dss:SetAsync(UniqueKey, toSave)
end
end)

game.Players.PlayerRemoving:connect(function(plr)
-- Saves data when player leaves the game.
local UniqueKey = 'id-'..plr.UserId
local d = plr.leaderstats
local savetable = {d.Rank.Value, d.RankVal.Value, d.Exp.Value}	 -- A table to hold/save all the values.

dss:SetAsync(UniqueKey,savetable)
end)

DataStoreService will store the Exp for you with this code. I hope it works. Leave a comment if you have any problems with this script.

6 comments

0

"UserID is not a valid member of Player"

0

which line?

0

its "UserId" not "UserID"

0

Actually that was a mistake on my part. There is no error for the output HOWEVER the script does not work. I tested it in a server and changed my rank manually, when I rejoined my Exp was 8, my rank was 8 and my class was Elder.

0

when u changed your rank, the game automatically saves it when u leave the game. All you have to do is to use developer console and change your rank back. Press F9 (or Fn + F9 for laptops) then go to Server Log. Then, type game.Players.YOURNAME.leaderstats.rank = YOURRANK

0

What rank did u change into? (To be honest) What rank do u wanna change into?

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