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

How do i load character when i unequip clicked?

Asked by 4 years ago

Hello Bros i made a game where i got my own inventory and when i unequip my accessory i want to load character when "Unequip" is clicked where should i add here? thanks in advance.

local dataStore = game:GetService("DataStoreService") local HatsData = dataStore:GetDataStore("HatDataSavingg") local module = {}

local currency = script:WaitForChild("Settings"):WaitForChild("Currency") local maxhats = script:WaitForChild("Settings"):WaitForChild("MaxEquippableHats") local cansavehats = false local alredysavedhats = true

function module.loadhats(player)

local maxhats = Instance.new("IntValue",player)
maxhats.Name = "maxHats"
maxhats.Value = script:WaitForChild("Settings"):WaitForChild("MaxEquippableHats").Value

local maxhatsinv = Instance.new("IntValue",player)
maxhatsinv.Name = "maxHatsInv"
maxhatsinv.Value = script:WaitForChild("Settings"):WaitForChild("MaxHatStorage").Value

local HatsFolder = Instance.new("Folder",player)
HatsFolder.Name = "Hats" 
local HatsTable = {}


local function dataload()
HatsTable = HatsData:GetAsync(player.UserId)
end

local sucess, err = pcall(dataload)

if sucess then


if not HatsTable or #HatsTable == 0 then
    alredysavedhats = false
end



local num1 = 1
local num2 = 2
local num3 = 3
local num4 = 4

if HatsTable then if #HatsTable >0 then for i = 1, #HatsTable / 4 do

                local hat = Instance.new("BoolValue",HatsFolder)
                    hat.Name = HatsTable[num1]
                    hat.Value = HatsTable[num2]

                local hatimage = Instance.new("NumberValue",hat)
                    hatimage.Name = "ImageID"
                    hatimage.Value = HatsTable[num3]

                local hatrarity = Instance.new("StringValue", hat)
                    hatrarity.Name = "Rarity"
                    hatrarity.Value = HatsTable[num4]


                if num4 == #HatsTable then
                    cansavehats = true
                end


                    num1 = num1 + 4
                    num2 = num1 + 1
                    num3 = num1 + 2
                    num4 = num1 + 3

    end
end

end else
print("Error while loading: "..err) player:Kick("Failed to load your data!")
end end

--Save Hats function module.savehats(player) if cansavehats == true or alredysavedhats == false then

    local HatsTableSave =  {}

for i,v in pairs(player:WaitForChild("Hats"):GetChildren()) do
    table.insert(HatsTableSave, v.Name)
    table.insert(HatsTableSave, v.Value)
    table.insert(HatsTableSave, v:WaitForChild("ImageID").Value)
    table.insert(HatsTableSave, v:WaitForChild("Rarity").Value)
end

    local function checking()
        HatsData:SetAsync(player.UserId, HatsTableSave)
    end

    local sucess, err = pcall(checking)

    if sucess then

    else
        print("Error while saving: "..err)
        local plr = player
        module.savehats(plr)
    end
end

end

function module.PlrJoinedAddHat(player) for i,v in pairs(player:WaitForChild("Hats"):GetChildren())do if v.Value == true then local clone = game.ServerStorage:WaitForChild("Hats"):WaitForChild(v.Name):Clone() clone.Name = v.Name

        local object = Instance.new("ObjectValue",v)
        object.Name = "Object"
        object.Value = clone

        repeat wait() until game.Workspace:FindFirstChild(player.Name)
        local char = game.Workspace:WaitForChild(player.Name)
        char:WaitForChild("Humanoid"):AddAccessory(clone)
    end
end

end

function module.deletehat(player,hat) hat:Destroy() end

function module.equipunequip(player,state,hat) if state == "Unequip" then hat.Value = false hat:WaitForChild("Object").Value:Destroy() hat:WaitForChild("Object"):Destroy()

elseif state == "Equip" then
    hat.Value = true        

    local clone = game.ServerStorage:WaitForChild("Hats"):WaitForChild(hat.Name):Clone()
    clone.Name = hat.Name

    local object = Instance.new("ObjectValue",hat)
    object.Name = "Object"
    object.Value = clone


    repeat wait() until game.Workspace:FindFirstChild(player.Name)
    local char = game.Workspace:WaitForChild(player.Name)
    char:WaitForChild("Humanoid"):AddAccessory(clone)
end

end

local hats = game.ServerStorage:WaitForChild("Hats") function getRandomRarity(hatshop) local tempRarityTable = {} for _, i in ipairs(hatshop:WaitForChild("Rarities"):GetChildren()) do for p = 0, i.Value do table.insert(tempRarityTable, i.name) end end return tempRarityTable[math.random(#tempRarityTable)] end

function getRandomHatByRarity(rarity) local tempTable = {} for _, i in ipairs(hats:GetChildren()) do if i:WaitForChild("Rarity").Value == rarity then table.insert(tempTable, i) end end return tempTable[math.random(#tempTable)] end

function module.buyhat(plr, hatshop, amount) if hatshop == nil then return end

local price = hatshop:WaitForChild("Price").Value
if plr:WaitForChild("leaderstats"):WaitForChild(currency.Value).Value >= price then
    if #plr:WaitForChild("Hats"):GetChildren() + amount > script:WaitForChild("Settings"):WaitForChild("MaxHatStorage").Value then return "Not enough space" end
    plr:WaitForChild("leaderstats"):WaitForChild(currency.Value).Value = plr:WaitForChild("leaderstats"):WaitForChild(currency.Value).Value - price
    local hatsOpened = {}

    for i = 1, amount do
        local rarity = getRandomRarity(hatshop)
        local hat = getRandomHatByRarity(rarity)

        local hatAdd = Instance.new("BoolValue",plr:WaitForChild("Hats"))
            hatAdd.Name = hat.Name
            hatAdd.Value = false
        local rarit = Instance.new("StringValue",hatAdd)
            rarit.Name = "Rarity"
            rarit.Value = rarity
        local imageid = Instance.new("NumberValue",hatAdd)
            imageid.Name = "ImageID"
            imageid.Value = game.ServerStorage:WaitForChild("Hats"):WaitForChild(hat.Name):WaitForChild("ImageID").Value


        table.insert(hatsOpened, hatAdd.Name)
        table.insert(hatsOpened, rarit.Value)
        table.insert(hatsOpened, imageid.Value)

    end

    return hatsOpened
else
    return "Insufficient Funds"
end

end

return module

Answer this question