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

Leaderstats string value automatically change?

So, I'm having trouble getting a leaderstats string value to change automatically after an IntValue changes in correspondence to this. How would I go about doing this?

I have:

datastore = game:GetService("DataStoreService"):GetDataStore("rating")

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Model", player)
	leaderstats.Name = "leaderstats"
	local rating = Instance.new("IntValue", leaderstats)
	rating.Name = "Rating"
	local ranking = Instance.new("StringValue", leaderstats)
	ranking.Name = "Rank"
	
	local key = "user_"..player.userId
	if (datastore:GetAsync(key) ~= 0) then
		rating.Value = datastore:GetAsync(key)
	else
		rating.Value = 1500
	end
	
	rating.Changed:connect(function(val)
		datastore:SetAsync(key, val)
	end)
	
	if (rating.Value <= 1500) then
		ranking.Value = "Rust I"
	elseif (rating.Value > 1500 and rating.Value <= 2000) then
		ranking.Value = "Rust II"
	elseif (rating.Value > 2000 and rating.Value <= 2500) then
		ranking.Value = "Rust III"
	end
end)

1 comment

0

Ooh, yeah, and the datastore itself is working fine.

1 answer

✓ Accepted answer

You aren't setting ranking.Value inside of the Changed connection. You put that part after it, so it only gets set once. To solve that, put it inside the Changed event and set the initial value after the event is connected.

datastore = game:GetService("DataStoreService"):GetDataStore("rating")

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("Model", player)
	leaderstats.Name = "leaderstats"
	local rating = Instance.new("IntValue", leaderstats)
	rating.Name = "Rating"
	local ranking = Instance.new("StringValue", leaderstats)
	ranking.Name = "Rank"

	local key = "user_"..player.userId

	rating.Changed:connect(function(val)
		datastore:SetAsync(key, val)
		if (rating.Value <= 1500) then
			ranking.Value = "Rust I"
		elseif (rating.Value > 1500 and rating.Value <= 2000) then
			ranking.Value = "Rust II"
		elseif (rating.Value > 2000 and rating.Value <= 2500) then
			ranking.Value = "Rust III"
		end
	end)
	
	local init=datastore:GetAsync(key)
	if (init ~= 0) then
		rating.Value = init
	else
		rating.Value = 1500
	end
end)

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