Issue updating a variables value only when the player joins

So in a nutshell, I have an IntValue that is datastored, and when the player joins it needs to be set to another IntValue's value. The problem is, that other IntValue changes throughout the game, so I’ve discovered doing it when the player joins and when the player leaves both end up with the value being the lowest value (as the value goes down with each word spoken in the game). For example, you could start with 50 words, but when you leave the datastore will save the IntValue as whatever the value may be when the player leaves or is kicked for using all of their words (lets say for example they leave with 3 words left) the PreviousWords IntValue's value will be 3. I need it to be set to whatever the starting amount of words was.

Script:

script.Parent = workspace
game.Players.PlayerAdded:Connect(function(new)
	local character = new.Character
	local LeaderStats = Instance.new("Folder", new)
	LeaderStats.Name = "LeaderStats"
	local stat = Instance.new("IntValue", LeaderStats)
	local PreviousWords = LeaderStats:WaitForChild("PlayerValues").PreviousWords
	stat.Name = "Words"
	stat.Value = LeaderStats:WaitForChild("PlayerValues").PreviousWords.Value + math.random(1, 50)
	
	

	new.Chatted:Connect(function(message)
		local words = string.split(message, " ")
		stat.Value -= #words
		if stat.Value <= 0 then new:Kick() end
	end)
	
	local gui = script.TextName:Clone()
	local check = game.Workspace:WaitForChild(new.Name)
	if check~=nil then
		local find = check:FindFirstChild("Head")
		if find ~= nil then	
			gui.Parent = find
			gui.Namer.Text = "Words Left: " .. new.LeaderStats.Words.Value
			new.LeaderStats.Words.Changed:Connect(function()
				gui.Namer.Text = "Words Left: " .. new.LeaderStats.Words.Value
			end)
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(new)
	local LeaderStats = new:FindFirstChild("LeaderStats")
	local PreviousWords = LeaderStats:WaitForChild("PlayerValues").PreviousWords
	local stat = LeaderStats:WaitForChild("Words")
	PreviousWords.Value = stat.Value
	print(PreviousWords.Value)
end)

isn’t this the starting number of words?

This part determines it based off of the value of previous words, it’s previous words + a random amount.

If you want to set the IntValue to be the original value that they started with in game, you’ll need to store the original value via a datastore and then when they join back, you set the IntValue to be the stored value

1 Like