Is There A More Convenient Way To Write This Script?

Hey developers!

I am currently trying to figure out how to make this script more convenient, as it is filled with a bunch of GetDataStores, Instance.news, and SetAsyncs. Is there any way to shorten this code and make it more convenient?

-- Leaderstats script in ServerScriptService 

local stat = "Tokens"
local startamount = 0

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")

local tomskinds = DataStore:GetDataStore("TomSkinSave")
local skeletomskin = DataStore:GetDataStore("SkeletomSkinSave")
local forsakentomskin = DataStore:GetDataStore("ForsakenTomSkinSave")

local whiteoutbobskin = DataStore:GetDataStore("WhiteoutBobSkinSave")
local madhatterbobskin = DataStore:GetDataStore("MathatterBobSkinSave")
local forsakenbobskin = DataStore:GetDataStore("ForsakenBobSkinSave")

game.Players.PlayerAdded:connect(function(player)

	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Cash = Instance.new("IntValue",leader)

	local monsterskins = Instance.new("Folder",player)
	monsterskins.Name = "monsterskins"

	local TomSkins = Instance.new("NumberValue",monsterskins)
	local SkeletomSkins = Instance.new("NumberValue",monsterskins)
	local ForsakenTomSkins = Instance.new("NumberValue",monsterskins)

	local WhiteoutBobSkins = Instance.new("NumberValue",monsterskins)
	local MadhatterBobSkins = Instance.new("NumberValue",monsterskins)
	local ForsakenBobSkins = Instance.new("NumberValue",monsterskins)

	Cash.Name = stat
	Cash.Value = ds:GetAsync(player.UserId) or startamount
	ds:SetAsync(player.UserId, Cash.Value)
	Cash.Changed:connect(function()
		ds:SetAsync(player.UserId, Cash.Value)



	end)

	TomSkins.Name = "AllEyesTomValue"
	local TomSkinStart = 0

	TomSkins.Value = tomskinds:GetAsync(player.UserId) or TomSkinStart
	tomskinds:SetAsync(player.UserId, TomSkins.Value)
	TomSkins.Changed:connect(function()
		tomskinds:SetAsync(player.UserId, TomSkins.Value)

	end)

	SkeletomSkins.Name = "SkeletomValue"

	SkeletomSkins.Value = skeletomskin:GetAsync(player.UserId)
	skeletomskin:SetAsync(player.UserId, SkeletomSkins.Value)
	SkeletomSkins.Changed:connect(function()
		skeletomskin:SetAsync(player.UserId, SkeletomSkins.Value)

	end)

	ForsakenTomSkins.Name = "ForsakenTomValue"

	ForsakenTomSkins.Value = forsakentomskin:GetAsync(player.UserId)
	forsakentomskin:SetAsync(player.UserId, ForsakenTomSkins.Value)
	ForsakenTomSkins.Changed:connect(function()
		forsakentomskin:SetAsync(player.UserId, ForsakenTomSkins.Value)

	end)

	WhiteoutBobSkins.Name = "WhiteoutBobValue"

	WhiteoutBobSkins.Value = whiteoutbobskin:GetAsync(player.UserId)
	whiteoutbobskin:SetAsync(player.UserId, WhiteoutBobSkins.Value)
	WhiteoutBobSkins.Changed:connect(function()
		whiteoutbobskin:SetAsync(player.UserId, WhiteoutBobSkins.Value)

	end)

	MadhatterBobSkins.Name = "MadhatterBobValue"

	MadhatterBobSkins.Value = madhatterbobskin:GetAsync(player.UserId)
	madhatterbobskin:SetAsync(player.UserId, MadhatterBobSkins.Value)
	MadhatterBobSkins.Changed:connect(function()
		madhatterbobskin:SetAsync(player.UserId, MadhatterBobSkins.Value)
	end)

	ForsakenBobSkins.Name = "ForsakenBobValue"

	ForsakenBobSkins.Value = forsakenbobskin:GetAsync(player.UserId)
	forsakenbobskin:SetAsync(player.UserId, ForsakenBobSkins.Value)
	ForsakenBobSkins.Changed:connect(function()
	forsakenbobskin:SetAsync(player.UserId, ForsakenBobSkins.Value)

	end)

end)

game.Players.PlayerRemoving:connect(function(player)

	ds:SetAsync(player.UserId, player.leaderstats.Tokens.Value)
	tomskinds:SetAsync(player.UserId, player.monsterskins.AllEyesTomValue.Value)
	skeletomskin:SetAsync(player.UserId, player.monsterskins.SkeletomValue.Value)
	forsakentomskin:SetAsync(player.UserId, player.monsterskins.ForsakenTomValue.Value)

	whiteoutbobskin:SetAsync(player.UserId, player.monsterskins.WhiteoutBobValue.Value)
	madhatterbobskin:SetAsync(player.UserId, player.monsterskins.MadhatterBobValue.Value)
	forsakenbobskin:SetAsync(player.UserId, player.monsterskins.ForsakenBobValue.Value)
end)
-- LocalScript inside of a ScreenGui, under the StarterGui

wait(5)
------------------------------------------------
if game.Players.LocalPlayer.monsterskins.AllEyesTomValue.Value == 1.12 then
	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.BorderSizePixel = 4
	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.TextButton.Text = "Unequip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("All Eyes equipped!")
	
elseif game.Players.LocalPlayer.monsterskins.AllEyesTomValue.Value == 1.1 then

	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.TextButton.Text = "Equip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.AllEyes.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("All Eyes In Inventory!")
	end

------------------------------------------------
if game.Players.LocalPlayer.monsterskins.SkeletomValue.Value == 1.22 then
	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.BorderSizePixel = 4
	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.TextButton.Text = "Unequip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Skeletom equipped!")

elseif game.Players.LocalPlayer.monsterskins.SkeletomValue.Value == 1.2 then

	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.TextButton.Text = "Equip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.Skeletom.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Skeletom in inventory!")
end
------------------------------------------------
if game.Players.LocalPlayer.monsterskins.ForsakenTomValue.Value == 1.32 then
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.BorderSizePixel = 4
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.TextButton.Text = "Unequip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Forsaken Tom equipped!")

elseif game.Players.LocalPlayer.monsterskins.ForsakenTomValue.Value == 1.3 then

	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.TextButton.Text = "Equip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenTom.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Forsaken Tom in inventory!")
end
------------------------------------------------
if game.Players.LocalPlayer.monsterskins.ForsakenBobValue.Value == 2.32 then
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.BorderSizePixel = 4
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.TextButton.Text = "Unequip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Forsaken Bob equipped!")

elseif game.Players.LocalPlayer.monsterskins.ForsakenBobValue.Value == 2.3 then

	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.TextButton.Text = "Equip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.ForsakenBob.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Forsaken Bob in inventory!")
end


------------------------------------------------
if game.Players.LocalPlayer.monsterskins.WhiteoutBobValue.Value == 2.12 then
	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.BorderSizePixel = 4
	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.TextButton.Text = "Unequip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Whiteout Bob equipped!")

elseif game.Players.LocalPlayer.monsterskins.WhiteoutBobValue.Value == 2.1 then

	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.TextButton.Text = "Equip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.Whiteout.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Whiteout Bob inventory!")
end
------------------------------------------------
if game.Players.LocalPlayer.monsterskins.MadhatterBobValue.Value == 2.22 then
	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.BorderSizePixel = 4
	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.TextButton.Text = "Unequip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Mad Hatter Bob equipped!")

elseif game.Players.LocalPlayer.monsterskins.MadhatterBobValue.Value == 2.2 then

	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.TextButton.Text = "Equip"
	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.TextButton.Equip.Enabled = true
	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.TextButton.PurchaseButton:Destroy()
	script.Parent.Shop.SkinsFrame.MonsterFrame.MadHatter.Parent = script.Parent.Inventory.SkinsFrame.MonsterSkins
	print("Mad hatter Bob inventory!")
end
------------------------------------------------
-- LocalScript named "PurchaseButton" inside of TextButton < AllEyes < MonsterFrame < SkinsFrame < Shop < ShopAndInventory
script.Parent.MouseButton1Click:Connect(function()
	if game.Players.LocalPlayer.leaderstats.Tokens.Value >= 50 then
		script.Parent.Purchase:Play()
		script.Parent.Text = "Equip"
		script.Parent.Equip.Enabled = true
		script.Parent.Parent.Parent = script.Parent.Parent.Parent.Parent.Parent.Parent.Inventory.SkinsFrame.MonsterSkins
		game.ReplicatedStorage.AllEyesRemove:FireServer()
		script:Destroy()
	end
end)
-- LocalScript named "Equip" inside of TextButton < AllEyes < MonsterFrame < SkinsFrame < Shop < ShopAndInventory
script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Text == "Equip" then
		script.Parent.EquipSound:Play()
		script.Parent.Parent.BorderSizePixel = 4
		script.Parent.Text = "Unequip"
		game.ReplicatedStorage.AllEyesEquip:FireServer()
	else
		script.Parent.UnequipSound:Play()
		script.Parent.Parent.BorderSizePixel = 0
		script.Parent.Text = "Equip"
		game.ReplicatedStorage.AllEyesEquip:FireServer()
	end
end)
-- Script named "RemoveTokens" inside of ServerScriptService
------------------------------------------------------
local function AllEyesPurchase(player)
	print("All Eyes Tom Purchased!")
	player.leaderstats.Tokens.Value -= 50
	player.monsterskins.AllEyesTomValue.Value = 1.1
	
end
local function AllEyesEquipped(player)
	if player.monsterskins.AllEyesTomValue.Value == 1.1 then
		print("All Eyes Tom Equipped!")
		
		player.monsterskins.AllEyesTomValue.Value = 1.12
		

	else
		print("All Eyes Tom Unequipped!")
		player.monsterskins.AllEyesTomValue.Value = 1.1
	end
end
------------------------------------------------------
local function SkeletomPurchase(player)
	print("All Eyes Tom Purchased!")
	player.leaderstats.Tokens.Value -= 500
	player.monsterskins.SkeletomValue.Value = 1.2

end
local function SkeletomEquipped(player)
	if player.monsterskins.SkeletomValue.Value == 1.2 then
		print("Skeletom Equipped!")
		player.monsterskins.SkeletomValue.Value = 1.22
		
	else
		print("Skeletom Unequipped!")
		player.monsterskins.SkeletomValue.Value = 1.2
	end
end
------------------------------------------------------
local function ForsakenTomEquipped(player)
	if player.monsterskins.ForsakenTomValue.Value == 1.3 then
		print("Forsaken Tom Equipped!")
		player.monsterskins.ForsakenTomValue.Value = 1.32

	else
		print("Forsaken Tom Unequipped!")
		player.monsterskins.ForsakenTomValue.Value = 1.3
	end
end
------------------------------------------------------
local function ForsakenBobEquipped(player)
	if player.monsterskins.ForsakenBobValue.Value == 2.3 then
		print("Forsaken Bob Equipped!")
		player.monsterskins.ForsakenBobValue.Value = 2.32

	else
		print("Forsaken Bob Unequipped!")
		player.monsterskins.ForsakenBobValue.Value = 2.3
	end
end
------------------------------------------------------
local function WhiteoutBobPurchase(player)
	print("Whiteout Bob Purchased!")
	player.leaderstats.Tokens.Value -= 50
	player.monsterskins.WhiteoutBobValue.Value = 2.1

end
local function WhiteoutBobEquipped(player)
	if player.monsterskins.WhiteoutBobValue.Value == 2.1 then
		print("Whiteout Bob Equipped!")
		player.monsterskins.WhiteoutBobValue.Value = 2.12

	else
		print("Whiteout Bob Unequipped!")
		player.monsterskins.WhiteoutBobValue.Value = 2.1
	end
end
------------------------------------------------------

local function MadhatterBobPurchase(player)
	print("Mad Hatter Bob Purchased!")
	player.leaderstats.Tokens.Value -= 500
	player.monsterskins.MadhatterBobValue.Value = 2.2

end
local function MadhatterBobEquipped(player)
	if player.monsterskins.MadhatterBobValue.Value == 2.2 then
		print("Mad Hatter Bob Equipped!")
		player.monsterskins.MadhatterBobValue.Value = 2.22

	else
		print("Mad Hatter Bob Unequipped!")
		player.monsterskins.MadhatterBobValue.Value = 2.2
	end
end

game.ReplicatedStorage.AllEyesRemove.OnServerEvent:Connect(AllEyesPurchase)
game.ReplicatedStorage.AllEyesEquip.OnServerEvent:Connect(AllEyesEquipped)

game.ReplicatedStorage.SkeletomRemove.OnServerEvent:Connect(SkeletomPurchase)
game.ReplicatedStorage.SkeletomEquip.OnServerEvent:Connect(SkeletomEquipped)

game.ReplicatedStorage.ForsakenTomEquip.OnServerEvent:Connect(ForsakenTomEquipped)

game.ReplicatedStorage.ForsakenBobEquip.OnServerEvent:Connect(ForsakenBobEquipped)

game.ReplicatedStorage.WhiteoutBobRemove.OnServerEvent:Connect(WhiteoutBobPurchase)
game.ReplicatedStorage.WhiteoutBobEquip.OnServerEvent:Connect(WhiteoutBobEquipped)

game.ReplicatedStorage.MadhatterRemove.OnServerEvent:Connect(MadhatterBobPurchase)
game.ReplicatedStorage.MadhatterEquip.OnServerEvent:Connect(MadhatterBobEquipped)

Here’s the model with all of the resources: here

Skins equip/unequip remote events:
image

Thank you for reading, and thank you to those who are willing to help me out! It means a lot! Thank you!

4 Likes

You could maybe try making an event called SkinChanged and make a IntValue where each skin has a corresponding value. Eg. Madhatter is 1, Whiteout Bob is 2 etc.

2 Likes

You don’t need a datastore for every single value, only need one

4 Likes

How would I make a single data store for all the skins?

2 Likes

Just make one datastore, create boolvalues and name them all to the specific things you wanted to save, and then just do a true value if they have it and a false value if they dont. Note: I haven’t taken a full look at your code, but just looking at the datastore I am assuming this is what you need to do.

Have a single datastore, and have different tables inside for more organization

local data_to_save = {
    Skins = {
        Skin1 = "Skin1"
        Skin2 = "Skin2"
    }
    Cash = 10
    Other = {}
}
2 Likes

Declare variables for things that are reused in the script (eg. ReplicatedStorage)

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