How would I add DataStore to this script?

I have a sword purchase script, the script I have provided below is a LocalScript that is inside of the sword purchase button, you can view it below.

LocalScript (PurchaseScript)

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local buyButton = script.Parent
local equipButton = script.Parent.EquipButton
local clickSound = ReplicatedStorage.GameSounds.ClickSound
local hoverSound = ReplicatedStorage.GameSounds.HoverSound

local sword_to_give = script.Parent.Name
local sword = ReplicatedStorage.Swords:FindFirstChild(sword_to_give)
local gemCost = script.Parent.Price.Value

local buyButtonConnection
local yesButtonConnection
local uiElement = Players.LocalPlayer.PlayerGui:WaitForChild("User Interface"):WaitForChild("UserUI").SwordPurchaseUI

local player = Players.LocalPlayer
local equippedSwordChangedEvent = game.ReplicatedStorage.Events:WaitForChild("EquippedSwordChangedEvent")
local swordPurchasedEvent = game.ReplicatedStorage.Events:WaitForChild("SwordPurchasedEvent")

local function checkAndReplaceSword()
	local backpack = player.Backpack
	for _, tool in ipairs(backpack:GetChildren()) do
		if tool:IsA("Tool") and tool.Name == sword_to_give then
			tool:Destroy()
		end
	end
end

local function changeImage()
	local imageLink = "rbxassetid://17316025753"
	local parent = script.Parent.Parent

	for _, child in ipairs(parent:GetChildren()) do
		local image = child:FindFirstChild("EquipButton")

		if image then
			image.Image = imageLink
		end
	end
end

local function TweenUI()
	uiElement.Position = UDim2.new(0.5, 0, 1.5, 0)
	uiElement.Size = UDim2.new(0, 0, 0, 0)
	uiElement.Visible = true
	uiElement.SwordImage.Image = script.Parent.ImageLabel.Image
	uiElement.SwordImageBg.Image = script.Parent.ImageLabel.Image
	game.Lighting.Blur.Enabled = true

	local tweenInfo = TweenInfo.new(
		1,
		Enum.EasingStyle.Quad,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)

	local targetProperties = {
		Position = UDim2.new(0.275, 0,0.258, 0),
		Size = UDim2.new(0, 589,0, 387),
	}

	local tween = TweenService:Create(uiElement, tweenInfo, targetProperties)
	tween:Play()
end

script.Parent.MouseEnter:Connect(function()
	hoverSound:Play()
end)

local function buyItem()
	clickSound:Play()
	script.Parent.Parent.Parent.Visible = false

	local confirmationUI = player.PlayerGui["User Interface"].UserUI.SwordConfirmationUI
	confirmationUI.SwordName.Text = script.Parent.Name

	if confirmationUI then
		confirmationUI.Visible = true
		confirmationUI.GemsCost.Text = gemCost
		confirmationUI.SwordImage.Image = script.Parent.ImageLabel.Image

		yesButtonConnection = confirmationUI.YesButton.MouseButton1Click:Connect(function()
			local checkGems = game.ReplicatedStorage.Functions.CheckGems:InvokeServer(gemCost)
			if checkGems then
				swordPurchasedEvent:FireServer(sword)
				confirmationUI.Visible = false
				equipButton.Visible = true
				TweenUI()
				game.ReplicatedStorage.GameSounds.PurchaseSound:Play()
			else
				print("Not enough gems")
			end
		end)

		local noButton = confirmationUI:FindFirstChild("NoButton")
		if noButton then
			noButton.MouseButton1Click:Connect(function()
				confirmationUI.Visible = false
				clickSound:Play()
				script.Parent.Parent.Parent.Visible = true
				if yesButtonConnection then
					yesButtonConnection:Disconnect()
				end
			end)
		end
	end
end

buyButton.MouseButton1Click:Connect(buyItem)

equipButton.MouseEnter:Connect(function()
	hoverSound:Play()
end)

equipButton.MouseButton1Click:Connect(function()
	clickSound:Play()
	if equipButton.Image == "rbxassetid://17316025753" then
		if sword_to_give then
			changeImage()
			equippedSwordChangedEvent:FireServer(sword_to_give) 
			equipButton.Image = "rbxassetid://17316048949"
		end
	elseif equipButton.Image == "rbxassetid://17316048949" then
		equipButton.Image = "rbxassetid://17316025753"    
	end
end)

Inside of this script, there is a RemoteEvent that fires whenever the player confirms the purchase, when the player purchases a sword, you can see in the LocalScript that the equip button becomes visible for them to equip a sword (equipping a sword just changes the value of EquippedSword inside of the player).

The server-side script that I’m using for the RemoteEvent is here;

Server-Side Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local swordPurchasedEvent = ReplicatedStorage.Events:WaitForChild("SwordPurchasedEvent")

local swordPrices = {
	["Spartan Sword"] = 500,
	["Steampunk Sword"] = 1000,
	["Skelly Slayer"] = 1500,
	["Signifier Sword"] = 2000,
	["Darkheart"] = 2750,
	["Overseer Wrath"] = 5000,
	["Serpent Sword"] = 6000,
	["Light Blade"] = 7500,
	["Plated Sword"] = 9250,
	["Uppercut Sword"] = 11000,
	["Evil Knight"] = 13450,
	["Arabian Knight"] = 16000,
	["Spec Elipson"] = 19000,
	["Interstellar Sword"] = 23500,
	["RGB Sword"] = 31000,
	["Violet Sword"] = 40000,
	["Gilded Sword"] = 52500,
	["Omi Odachi Sword"] = 65000,
	["Orc Blade"] = 80000,
	["Sorcus Sword"] = 97500,
	["Sword Of Darkness"] = 111000,
	["Amethyst Rock Sword"] = 125000,
	["Blood Sword"] = 200000,
	["Molten Scythe"] = 250000,
}

swordPurchasedEvent.OnServerEvent:Connect(function(player, sword)
	print("Received sword:", sword)
	local price = swordPrices[tostring(sword)]
	print("Price:", price)
	if price then
		if player.leaderstats.Gems.Value >= price then
			print("Deducting gems...")
			player.leaderstats.Gems.Value = player.leaderstats.Gems.Value - price
			print("Gems deducted successfully.")
		else
			print("Player does not have enough gems to purchase.")
		end
	else
		print("Sword price not found.")
	end
end)

However, the issue is I would like to use DataStore so that all purchased swords save, for example, if I purchase a sword and leave and rejoin, I would like the “EquipButton” to be visible so I can equip the sword.

I have no insight or idea on how I would do so within this type of script, I’m a beginner and very rarely work with DataStore. An insight or tips/advice on how I would use DataStore within this script would be greatly appreciated, thank you.

Adding on; I was also thinking of creating a “OwnedSwords” folder that gets put into the Player when they join and whenever a player purchases a sword, a new “StringValue” with the purchased swords name gets put into the OwnedSwords folder and the stringValues save. And then, whenever the player joins the LocalScript checks whats ownedSwords the player has and then makes the equipButton visible for all the swords that they already own. Let me know if this is a good idea, thanks.

1 Like

Bump, still need help with this if possible. :slight_smile:

yes, it’s a good idea. You can try making this

1 Like

Thanks alot, I’ll try this out and let you know how it goes.

1 Like