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

Why doesn't it save my player Body Depth Scale And Width Scale? PLEASE INCLUDE A SCRIPT


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


game.Players.PlayerAdded:connect(function(player)
	local l = Instance.new("IntValue",player)
	l.Name = "leaderstats"
	local money = Instance.new("IntValue",l)
	money.Name = "Size"
DataHandler(player,money,"Size")
end)
game.Players.PlayerAdded:connect(function(player)
	local plr = game:GetService([[Players]]).LocalPlayer	
	local Character = plr.Character
	local Humanoid = Character:FindFirstChild('Humanoid')
DataHandler(player,Humanoid,"BodyDepthScale", "BodyWidthScale")
end)

4 comments

0

Since this seems like a server script, you shouldn't be using LocaPlayer in the last PlayerAdded function. Instead use the player argument. Also, you shouldn't be using data persistence anymore.

0

Then how do I save the size?

0

Using DataStores http://wiki.roblox.com/index.php/Data_store, you can combine the values into a table and store the table

0

This doesnt help me at all. How do I make tables and store my data in them?

2 answers

Lots wrong with your code.

First, you are wrapping your if statements in brackets (like in lines 10, 13, 22) when the brackets are specifically for math, and you have wrapped in brackets lines 11-12. You use brackets in these situations:

local forty = 5(8)

print(forty) — returns 40 since wrapping only 1 number in brackets multiplies the other by the wrapped number

And to get the magnitude of BasePart positions:

local p = game.Workspace.Part
local p2 = p.Parent.Part2
local magnitude = (p.Position - p2.Position).Magnitude

print(magnitude)

Next error is deprecated code. RBXScriptSignal:connect() is deprecated, switch to RBXScriptSignal:Connect(). Using deprecated code can cause problems, such as, but no limited to:

Lag

Delay

Code simply not working

6 comments

0

Where am I supposed to put these scripts?

1

Those were just examples for how and when to use the brackets.

0

What do u mean magnitude

0

Im just asking why this code isnot working

0

Magnitude is the distance between a certain Vector3 position and another Vector3 position.

0

Parentheses are not just for math. They can be used to change operator precedence. Also, Lua does not support x(y)

I though you quit scripting roblox and quit developing and i though you said its not for you

By: Soccerdeadmatchteam / ReaperSoul

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