Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with Pcall function and improvement of code [?]

Asked by 6 years ago

Goal:

My goal is to create a CC (Character Customization) System. I want the data to be transferred between places. With this set up: https://gyazo.com/c4f261f4b3538b5ee35d3c5dd3d86234

Problem:

I'm trying to improve this code and understand pcall function. Roblox Wiki isn't helping much in explaining how it works or where I should place the pcall function. I'm also trying to make this script save a lot of values in a small script (Probably using an in pairs loop).

Script:

This is my full script in ServerScriptService:

--// Settings \\--
local SAVE_TIME = 120 -- In Seconds

--// Variables \\--
local DSS = game:GetService('DataStoreService')
local CCDS = DSS:GetDataStore('Custom Character Data Store')

--// Functions \\--
local function GenerateKey(plr)
    local Key = "UserId_"..plr.UserId
    return Key
end

local function GenerateDataFromPlayer(plr)
    local DataTable = {
        Skin_Color = plr.CharacterData.Skin_Color.Value,
        Shirt_Color = plr.CharacterData.Shirt_Color.Value,
        Pants_Color = plr.CharacterData.Pants_Color.Value,
    }
    return DataTable
end

local function SavePlayerData(plr)
    local key = GenerateKey(plr)
    local data = GenerateDataFromPlayer(plr)
    CCDS:SetAsync(key, data)
end

local function LoadPlayerDataFromDataStore(plr)
    local key = GenerateKey(plr)
    local data = CCDS:GetAsync(key)
end

local function InputPlayerData(plr, data)
    local Char = plr.Character or plr.CharacterAdded:Wait()
    plr.CharacterData.Skin_Color.Value = data.Skin_Color
    plr.CharacterData.Shirt_Color.Value = data.Shirt_Color
    plr.CharacterData.Pants_Color.Value = data.Pants_Color
    Char:WaitForChild('Body Colors').HeadColor = BrickColor.new(plr.CharacterData.Skin_Color.Value)
    Char['Body Colors'].LeftArmColor = BrickColor.new(plr.CharacterData.Skin_Color.Value) 
    Char['Body Colors'].RightArmColor = BrickColor.new(plr.CharacterData.Skin_Color.Value)
    Char['Body Colors'].LeftLegColor = BrickColor.new(plr.CharacterData.Pants_Color.Value)
    Char['Body Colors'].RightLegColor = BrickColor.new(plr.CharacterData.Pants_Color.Value)
    Char['Body Colors'].TorsoColor = BrickColor.new(plr.CharacterData.Shirt_Color.Value)
end

--// Values \\--
game.Players.PlayerAdded:Connect(function(plr)

    local CharacterData = Instance.new('Folder')
    CharacterData.Name = 'CharacterData'
    CharacterData.Parent = plr

    local Skin_Color = Instance.new('StringValue')
    Skin_Color.Name = 'Skin_Color'
    Skin_Color.Value = 'Pastel brown'
    Skin_Color.Parent = CharacterData

    local Shirt_Color = Instance.new('StringValue')
    Shirt_Color.Name = 'Shirt_Color'
    Shirt_Color.Value = 'Fossil'
    Shirt_Color.Parent = CharacterData

    local Pants_Color = Instance.new('StringValue')
    Pants_Color.Name = 'Pants_Color'
    Pants_Color.Value = 'Brown'
    Pants_Color.Parent = CharacterData

    LoadPlayerDataFromDataStore(plr)
    InputPlayerData(plr, GenerateDataFromPlayer(plr))

    spawn(function()
        wait(2)
        for _, Values in pairs(CharacterData:GetChildren()) do
            Values.Changed:Connect(function()
                SavePlayerData(plr)
            end)
        end
    end)
    spawn(function()
        while wait(SAVE_TIME) do
            SavePlayerData(plr)
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function()
    SavePlayerData()
end)

So how can I add a p call function? How can I improve this script and would the data transfer between places and games as it is now? How can I improve it so I can save a lot of data at once?

https://gyazo.com/c4f261f4b3538b5ee35d3c5dd3d86234

That is my setup again.

Thank you for reading. I hope I can get some helpful feedback. Any feedback is appreciated.

0
If you don't know where to put your pcall, you probably don't need a pcall. hiimgoodpack 2009 — 6y

Answer this question