How to serialize wires

hi! Im developing the game “Into The Sea” and i want to add electric and signal wires but i dont know how to serialize them.

Actually in one folder are the wires and in another the models, how could i serialize the data in other table, not in the models table and later get what models are assigned in the wire?

I think i can do this saving the position of the model in the wires table but there are other options?

1 Like

What do you mean by serialize is this for like a placement system?

Serialize is convert something that cant be saved(parts, models, etc) into something that can be saved and later loaded(table) one example of this would be a placement system, the data of each model are stored in one table that will be saved and when the players join to the game u just load the data saved

You could assign an id to each socket. And then connect the wires to the ID’s that are connected in your save data. As far as how you would draw that wire, and if you’d like to save it the exact way the player setup is a completely different story.

hmmm but how can i assign the ids?

You could possibly use HttpService:GenerateGUID. It generates a completely unique string, and it may be very useful in this case.

I was working on a furniture placement system and here’s how I assigned IDs to each item:

Every item basically has an ID attached to it which behaves like an index, like “0004” or “0321”. When a new item is created, there’s a function that loops through all the items and finds the latest ID, then adds 1. This way every item has a unique id.

Examples for a function that gets a new id
local function GetFreshId(items)
    local latest = 0
    for index,item in pairs(items) do
        local numid = tonumber(item.ItemId) -- You don't need "tonumber" if your ID is already a number
        latest = math.max(latest, numid) -- So when the loop ends, you have the highest ID in the whole "items" list
    end
    return latest + 1 -- Add one so now it's a unique ID that hasn't been used yet
end
Minor downside

The one downside is that deleting items leaves a gap in the IDs. This doesn’t affect functionality whatsoever, it just means that the number in the IDs might go a bit higher than they need to be.

In your case, I’d assign an ID to each model that wires can connect to, then just add two properties to each wire indicating what model they’re connected to.

For example, a table for a wire could be:

["Wire0001"] = {
    Attachment1 = "Model0001",
    Attachment2 = "Model0002",
}

Then when loading in the data when the player rejoins, for example, you would first spawn all the models in, then at the very end create wires with attachments to those models.

tysmmmmmmm you just gave me an idea

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.