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: for _,v in pairs problem...

Previous Thread :: Next Thread 
eSvena is not online. eSvena
Joined: 11 Aug 2011
Total Posts: 75
23 Oct 2011 08:47 AM
Good afternoon !

I'm working on a script but I have an error that I can't fix... I need help :


function DataInitialization()

DataModel = Instance.new("Model")
DataModel.Parent = game:GetService("Debris")
DataModel.Name = "DataModel"

DataInitialization_Loaded = true

print("Data_Initialized")
end

function SaveData()

if (DataInitialization_Loaded == true) then

for _,v in pairs(game.Workspace:GetChildren()) do

if not(v:IsA("Camera") and v ~= script) then

if (DataModel ~= nil) then

v:clone().Parent = DataModel
print("Data_Saved")

end

end

end

else
print("Data_Not_Initialized")
return end

end

DataInitialization()
SaveData()

Output :

Workspace.Script:x line: attempt to index a nil value

I think that the nil valus is v but I declared it in my loop for...

I wish anyone could help me.

Thanks !
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
23 Oct 2011 09:10 AM
I think that by

    not(v:IsA("Camera") and v ~= script)

you meant

    not v:IsA("Camera") and v ~= script
Report Abuse
eSvena is not online. eSvena
Joined: 11 Aug 2011
Total Posts: 75
23 Oct 2011 09:13 AM
These lines hasn't got error. You can use () or not. See where the output find the error

Sorry for my bad grammar but I'm not english ^^

function SaveData()

if (DataInitialization_Loaded == true) then

for _,v in pairs(game.Workspace:GetChildren()) do

if not(v:IsA("Camera") and v ~= script) then

if (DataModel ~= nil) then

v:clone().Parent = DataModel ----> ERROR LINE : Attempt to call a nil value
print("Data_Saved")

end

end

end

else
print("Data_Not_Initialized")
return end

end
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
23 Oct 2011 09:15 AM
Global variables are normally bad practice. You'd do better to use arguments and return values. Also, `DataInitialization_Loaded` was redundant. You knew that the data was loaded, because `DataModel` would be non-nil

Here's how I'd do it:

    function InitializeDatastore()
        local dataStorage = Instance.new("Model")
        dataStorage.Parent = game:GetService("Debris")
        dataStorage.Name = "DataModel"
        print("Data_Initialized")
        return dataStorage;
    end
    
    function SaveDataTo(dataStorage)
        if dataStorage then
            for _, child in ipairs(game.Workspace:GetChildren()) do
                if not child:IsA("Camera") and child ~= script then
                    v:clone().Parent = dataStorage
                    print("Data_Saved")
                end
            end
        else
            error("No datastore specified!")
        end
    end
    
    local db = InitializeDatastore()
    SaveDataTo(db)
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
23 Oct 2011 09:16 AM
Whoops. Forgot to replace that `v` with `child`:

    function InitializeDatastore()
        local dataStorage = Instance.new("Model")
        dataStorage.Parent = game:GetService("Debris")
        dataStorage.Name = "DataModel"
        print("Data_Initialized")
        return dataStorage;
    end
    
    function SaveData(dataStorage)
        if dataStorage then
            for _, child in ipairs(game.Workspace:GetChildren()) do
                if not child:IsA("Camera") and child ~= script then
                    child:clone().Parent = dataStorage
                    print("Data_Saved")
                end
            end
        else
            error("No datastore specified!")
        end
    end
    
    local db = InitializeDatastore()
    SaveData(db)
Report Abuse
eSvena is not online. eSvena
Joined: 11 Aug 2011
Total Posts: 75
23 Oct 2011 09:20 AM
Ok thanks !

I'm learning scripting then I'm not very very good ^^ But when you write return dataStorage, this line will return a boolean value ?
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
23 Oct 2011 09:21 AM
> These lines hasn't got error

It's not an error. It's a logic problem.

`not(v:IsA("Camera") and v ~= script)` does not mean the same as `not v:IsA("Camera") and v ~= script`

The first says "v is not both a camera and something that isn't this script", whereas the second says "v is neither a camera nor this script".

It may not be the cause of your problem, but you definitely meant to write the second one.
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
23 Oct 2011 09:22 AM
> this line will return a boolean value

Which line? What makes you thing it returns a boolean value?
Report Abuse
eSvena is not online. eSvena
Joined: 11 Aug 2011
Total Posts: 75
23 Oct 2011 09:30 AM
The function DataInitialization() will return dataStorage; by a boolean value ( true of false ) ?

Sorry for my bad grammar, I'm not english.
Report Abuse
eSvena is not online. eSvena
Joined: 11 Aug 2011
Total Posts: 75
23 Oct 2011 09:44 AM
Never mind... But I tested your script and... don't work : attempt to index a nil value
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
23 Oct 2011 09:56 AM
`InitializeDatastore()` returns a Model. Have a read through it again.

In `SaveDataTo()`, I have the code `if dataStore then`. This _doesn't_ just check if `dataStore ~= false`. It actually checks that `dataStore ~= nil`.

So if you have `variable ~= nil` anywhere, and `variable` is not a boolean, then replace it with `variable`.
Report Abuse
eSvena is not online. eSvena
Joined: 11 Aug 2011
Total Posts: 75
23 Oct 2011 10:00 AM
Ok, thanks.
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