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

How to set value of IntValue from another IntValue?

Basically, I have leaderboardstats model and an IntValue in there so that money will show up on the leaderboards, but this is just for "show". The real values are to be in ReplicatedStorage. I use datastore to set the value of the replicatedstorage and it works fine, the problem is I then want the IntValue in leaderboardstats to match the IntValue in ReplicatedStorage. Here is my code

local DataStore = game:GetService("DataStoreService"):GetDataStore("data")

game.Players.PlayerAdded:connect(function(player)	
	local entry = player.Name
	local model = Instance.new("Model", game.ReplicatedStorage)
	local coins = Instance.new("IntValue", game.ReplicatedStorage.model)
		coins.Name = "Coins"
	local levels = Instance.new("IntValue")
		levels.Parent = game.ReplicatedStorage.model
		levels.Name = "XP"	
		model.Name = entry --This needs to be at the end
		-- These next values are for the leaderboards, this is just so I don't have to make a GUI for the leaderstats, only coins and xp matter
	local leaderboardstats = Instance.new("Model", player)
		leaderboardstats.Name = "leaderstats"
	local coins2 = Instance.new("IntValue")
		coins2.Parent = leaderboardstats
		coins2.Name = "Coins"
	local levels2 = Instance.new("IntValue")
		levels2.Parent = leaderboardstats
		levels2.Name = "XP"
	wait(1) 
	local key = "player-"..player.userId
	local savedValues = DataStore:GetAsync(key)
	if savedValues then
		--Save format: (status, xp, hp, s, etc)
		coins.Value = savedValues[1] or 0 
		levels.Value = savedValues[2] or 0
	else
		local valuesToSave = {coins.Value, levels.Value}		
		DataStore:SetAsync(key, valuesToSave)						
	end	
	wait(1)
	player.leaderstats.Coins.Value = game.ReplicatedStorage.entry.Coins.Value --Sync the Coins IntValue in ReplicatedStorage with one in leaderstats
	player.leaderstats.XP.Value = game.ReplicatedStorage.entry.XP.Value -- Sync the XP IntValue in ReplicatedStorage with one in leaderstats

2 answers

Well I can't be sure, but I don't think your suppose to keep things your using in ReplicatedStorage. I'm not sure if that is the problem though. Have you tried debugging it? Make sure you print the values in ReplicatedStorage to make sure they are what you think they are.

Your issue is this:

You have a variable, entry whose value is the new players name. You want to find an object in ReplicatedStorage whose name is the same, so you did ReplicatedStorage.entry, but that finds am object called entry in ReplicatedStorage. What you actually want is ReplicatedStorage[entry]

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