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

"chickenBux" in leaderstats dataStore code showing up as "Value" in leaderstats?

I'm making a dataStore for my leaderstats, but the "chickenBux" part of the code shows up as "Value" in the leaderstats. Here's the code:

local DataStore = game:GetService("DataStoreService")
local ds1 = DataStore:GetDataStore("chickenEaten")
local ds2 = DataStore:GetDataStore("chickenBux")

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder", player)
	leader.Name = "leaderstats"
	
	local chickenEaten = Instance.new("IntValue", leader)
	chickenEaten.Name = "Chicken"
	chickenEaten.Value = ds1:GetAsync(player.UserId) or 0
	ds1:SetAsync(player.UserId, chickenEaten.Value)
	chickenEaten.Changed:connect(function()
		print('Saving Data...')
		ds2:SetAsync(player.UserId, chickenEaten.Value)
	end)
	--where I think the problem is
	local chickenBux = Instance.new("IntValue", leader)
	chickenBux.Name = "ChickenBux"
	chickenBux.Value = ds2:GetAsync(player.UserId) or 0
	ds2:SetAsync(player.UserId, chickenBux.Value)
	
	chickenBux.Changed:connect(function()
		print('Saving Data...')
		ds2:SetAsync(player.UserId, chickenBux.Value)
	end)
	
end)

game.Players.PlayerRemoving:connect(function(player)
	ds1:SetAsync(player.UserId, player.leaderstats.Chicken.Value)
	ds1:SetAsync(player.UserId, player.leaderstats.ChickenBux.Value)
end)

Thanks!

2 comments

0

Try using 1 data store

0

I just tried that & it didn't work. Probably a good idea, though

1 answer

✓ Accepted answer

I GOT IT!!!

Forget the leaderstats script and the DataStore, we are going to join both! Delete the leaderstats and the DataStore scripts I gave you, and insert a regular script, and copy the following!

--Ignore until line 30
function DataHandler(p,o,key)	-- DataHandler(Player, Object [, Key])
	local types = {
		IntValue = "Number";
		NumberValue = "Number";
		ObjectValue = "Instance";
		BoolValue = "Boolean";
		StringValue= "String";
	}
	local t = types[o.ClassName]
	if (not t) then return end
	key = (type(key) == "string" and key or o.Name)	-- If it's a string, keep it the same, ELSE change it to the object's name. Allows 'key' to be an optional argument.
	key = (#key > 0 and key or key.Name == o.Name and o.ClassName or o.Name)	-- If length of string is greater than 0, keep it the same
	if (not p.DataReady) then														-- ELSE if it is equal to the object's name then that means that the
		p:WaitForDataReady()														-- object's name is blank, therefore make 'key' the ClassName.
	end																			-- ELSE make it the name of the object.
	local Save,Load = p[("Save%s"):format(t)],p[("Load%s"):format(t)]	-- Steal the load and save functions from the player
	o.Value = Load(p,key)	-- The same as p:LoadTYPE(key). Since it's no longer a method, the player must be the first argument
	local lastV = o.Value
	o.Changed:connect(function(v)
		lastV = v
		delay(1,function()						-- Give a 1 second buffer to see if the value changes anymore. If so, stop the operation. If it goes 1 second without changing, it will automatically save.
			if (lastV ~= v) then return end	-- This way you don't save a value 20 times in 1 second if it changes constantly. Lower data processing = less lag
			Save(p,key,v)
		end)
	end)
end


--A table for holding all the player's leaderstat values
local playerLeaderstats = {} 
 
game.Players.PlayerAdded:connect(function(player)
	playerLeaderstats[player] = {}
	playerLeaderstats[player]["chickenEaten"] = 0
	local leaderstats = Instance.new("Model")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local chickenEaten= Instance.new("IntValue")
	chickenEaten.Name = "chickenEaten"
	chickenEaten.Value = playerLeaderstats[player]["chickenEaten"]
	chickenEaten.Parent = leaderstats

	playerLeaderstats[player]["chickenBux"] = 0
	local chickenBux= Instance.new("IntValue")
	chickenBux.Name = "chickenBux"
	chickenBux.Value = playerLeaderstats[player]["chickenBux"]
	chickenBux.Parent = leaderstats
	
	DataHandler(player,chickenEaten,"chickenEaten")
	DataHandler(player,chickenBux,"chickenBux")
end)

This script holds both leaderstats and their DataStore.

THIS WORKS VERIFIED. C'mon man after all this work you gotta mark this as the solution! :v

As always, good scripting!

22 comments

0

Thanks! The saving part doesn't work for some reason. (I went into a game, changed the value of "chickenBux", and rejoined. The value didn't save & was 0)

0

I think it doesn't work on Roblox Studio, only into Game. If it still won't work like that, change line 19 to "playerMoney[player] = chickenEaten" and add another below that would be "playerMoney[player] = chickenBux"

0

Which script would I put that in? (I assume the second script). When I put it in the second script, there was no such thing as chickenEaten or chickenBux

0

no, what you gotta do is replace the line 19 of the current script with "playerMoney[player] = chickenEaten"; And after that, enter, and write "playerMoney[player] = chickenBux"

0

I already did that, but chickenEaten and chickenBux weren't defined in the script

0

repaste the script there up, I just edited it rn.

0

I pasted it & there was no error, but the game still doesn't save for some reason.

0

now that's strange...

0

i think I'm finding a way... hold a sec.

0

DONE, I EDITED EVERYTHING, TRY THAT

0

Alright, I'm gonna mark asa the answer because you did so much work. So I just paste everything from like 30 to line 52? DataHandler is an unknown, and so are bux and gold (although i replaced them with chickenBux and chickenEaten, since that seemed like it made sense) thanks so much

0

no dude, you need to paste the whole script, you just wouldn't understand what's written until line 30 xD so you could just ignore it, and if you wanted, try to understand the rest.

0

Listen, simply paste the whole script, and look at it work XD

0

After all, I always like working xD

0

I pasted the whole script & the leaderstats didn't load in (chickenbux & chickeneaten won't show)

0

Strange!!! Any errors?

0

and has that anything to do with THIS script?

0

Well, whenever I test it in studio, the script above is replaced by your old script for some reason

0

And when I place it into a new script, it just turns into print("Hello world") when it runs

0

wat xD that's not my script's problem it's something on your studio xD

0

yassss it works thanks so much!

0

NP!!! Glad all these hours didn't go to waste! You know what, I enjoyed it. When you need more help, tell me. I also added you on RBLX! ;D

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