generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Data handler

Previous Thread :: Next Thread 
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 01:32 AM
I need help with this script, the script doesn't save the leaderstats.
----------------------------------------------------------------------------------------------------
game.Players.PlayerAdded:connect(function(player)
local l = Instance.new("IntValue",player)
l.Name = "leaderstats"
local money = Instance.new("IntValue",l)
money.Name = "Shards"
DataHandler(player,Shards,"Shards")
end)

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

--[[
-- Example:

game.Players.PlayerAdded:connect(function(player)
local l = Instance.new("IntValue",player)
l.Name = "leaderstats"
local money = Instance.new("IntValue",l)
money.Name = "Money"
DataHandler(player,money,"TheMoney")
end)
]]
Report Abuse
Cheater is not online. Cheater
Joined: 29 Jun 2007
Total Posts: 5258
15 Jun 2012 02:28 AM
LoadBoolean
LoadNumber
LoadString
LoadInstance
SaveBoolean
SaveNumber
SaveString
SaveInstance


I thought you may only use one of these in a Data Persistence script. I can't see any of these in your script.
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
15 Jun 2012 08:00 AM
That's because he's generating the names programatically - when `t == "String"`:

    p[("Save%s"):format(t)] == p.SaveString
Report Abuse
Cheater is not online. Cheater
Joined: 29 Jun 2007
Total Posts: 5258
15 Jun 2012 08:02 AM
Oh, maybe next time I should take a look at the whole script. I just searched for something like that. Sorry.
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
15 Jun 2012 08:06 AM
Your error is `key.Name == obj.Name`. Key is a string, so has no `.Name` property.
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
15 Jun 2012 08:11 AM
I would simplify your checking of `key`. There's not much point checking for an empty string, since you'll just end up with a name collision anyway:

    game.Players.PlayerAdded:connect(function(player)
        local l = Instance.new("IntValue",player)
        l.Name = "leaderstats"
        local money = Instance.new("IntValue",l)
        money.Name = "Shards"
        DataHandler(player,Shards,"Shards")
    end)
    
    function DataHandler(player, obj, key)
        local types = {
            IntValue = "Number";
            NumberValue = "Number";
            ObjectValue = "Instance";
            BoolValue = "Boolean";
            StringValue = "String";
        }
        local t = types[obj.ClassName]
        if not t then error( ("Type %q not saveable"):format(obj.ClassName) ) end
    
        --Allows 'key' to be an optional argument.
        key = key or obj.Name
    
        if not player.DataReady then
            player:WaitForDataReady()
        end
        -- Steal the load and save functions from the player
        local Save, Load = player["Save"..t], player["Load"..t]
    
        -- The same as player:LoadTYPE(key). Since it's no longer a method, the player must be the first argument
        obj.Value = Load(player, key)
    
        local lastV = obj.Value
        obj.Changed:connect(function(v)
            lastV = v
    
            -- 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. This way you don't save a value 20 times in 1 second if it changes constantly. Lower data processing = less lag
            Delay(1, function()
                if lastV ~= v then return end
                Save(player,key,v)
            end)
        end)
    end

Also, please put your comments on a new line, rather than wrapping comments in a column on the right over multiple lines. It makes it readable on all screen sizes.
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 01:11 PM
@NXT, it doesn't work.
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 01:34 PM
bump
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 01:52 PM
Can anyone help me with the script?
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 02:18 PM
bump again
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 02:59 PM
Can anyone help?
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
15 Jun 2012 03:45 PM
Output?
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 04:42 PM
absolutely nothing happens. It just doesn't do anything
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
15 Jun 2012 05:03 PM
Put print statements in various places and see what fails then.
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 05:48 PM
I tried putting print in a few places... It still didn't do anything, or maybe I just placed them in wrong places...
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
15 Jun 2012 06:08 PM
Put one in the top of the script, one in the playeradded, one at the start of your function, one before DataReady, one after data ready, and one inside the Changed event.
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 07:18 PM
Still nothing
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
15 Jun 2012 07:26 PM
How are you testing this? Also are you sure you have the output window open?
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 07:50 PM
It's not exactly nothing, but it isn't much to go on with...
19:48:21 - Workspace.DataHandler:17: attempt to index local 'obj' (a nil value)
19:48:21 - Script "Workspace.DataHandler", Line 17 - global DataHandler
19:48:21 - Script "Workspace.DataHandler", Line 6
19:48:21 - stack end
19:48:21 - Disconnected event because of exception
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
15 Jun 2012 07:57 PM
What are you passing into the function? Because it appears that he is expecting you to pass in a form of an ObjectValue.
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 09:13 PM
@ray I used the exact script he posted after he edited the script, then put a few print functions where he told me to place them.
Report Abuse
HatModder is not online. HatModder
Joined: 05 Jun 2012
Total Posts: 12891
15 Jun 2012 10:48 PM
bump
Report Abuse
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
15 Jun 2012 11:14 PM
Oh hey, that's my script
Report Abuse
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
15 Jun 2012 11:15 PM
Your problem is how Lua is compiled (JIT).

You need to put the PlayerAdded event function BELOW the big chunk DataHandler
Report Abuse
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
15 Jun 2012 11:17 PM
Also your function call was wrong:

DataHandler(player,Shards,"Shards")

Shards is not your object, money is. Change it to:


DataHandler(player,money,"Shards")
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image