I am trying to add a Gem Tool system where some swords only cost a currency while other swords need gems and some currency to buy them. How can I do that?
local RS = game:GetService("ReplicatedStorage") --I defined the replicated storage here
RS.BuyItem.OnServerEvent:Connect(function(player, price, toolName, button) --When the event fire, do the following commands:
if(not player.Backpack:FindFirstChild(toolName) and not player.Character:FindFirstChild(toolName)) then --If the player does not have the tool
if(player.leaderstats.Gold.Value >= price) then --And if he has more or the same cash than the price
player.leaderstats.Gold.Value = player.leaderstats.Gold.Value - price --Decrease his cash amount by the price
local item = RS.ShopItems[toolName]:Clone() --Clone the tool
item.Parent = player.Backpack --Put it in the player's backpack
button.BackgroundColor3 = Color3.fromRGB(255, 0, 0) --Set the button's color to red
button.Text = "Bought!" --Set the button's text to "Bought!"
wait(1) --Wait one second
button.BackgroundColor3 = Color3.fromRGB(85, 170, 0) --Set the color back to green
button.Text = "Buy" --Change the text to "Buy"
end
end
end)
Hey inquisitor! Sorry I’m on mobile right now so I can’t type exact code but I can give you an idea. In each tool, insert a string value named “CurencyName” and set the value to either Coins or Gems.
Then on the local script check the tool:FindFistChild(“CurrencyName”).Value, and fire the server also with that value.
Then on the server side, make a parameter for the String Value’s Valur named currencyname. Instead of decreasing the coins do this: player.leaderstats:FindFirstChild(currencyname).Value -= price
I added the or Gem script part to check if they have any of the gems and if they do then the script would proceed. So would this work?
local RS = game:GetService("ReplicatedStorage") --I defined the replicated storage here
RS.BuyItem.OnServerEvent:Connect(function(player, price, toolName, button) --When the event fire, do the following commands:
if(not player.Backpack:FindFirstChild(toolName) and not player.Character:FindFirstChild(toolName)) then --If the player does not have the tool
if(player.leaderstats.Gold.Value >= price and player:WaitForChild("BlueGem") or player:WaitForChild("GreenGem") or player:WaitForChild("RedGem") then --And if he has more or the same cash than the price---
player.leaderstats.Gold.Value = player.leaderstats.Gold.Value - price --Decrease his cash amount by the price
local item = RS.ShopItems[toolName]:Clone() --Clone the tool
item.Parent = player.Backpack --Put it in the player's backpack
button.BackgroundColor3 = Color3.fromRGB(255, 0, 0) --Set the button's color to red
button.Text = "Bought!" --Set the button's text to "Bought!"
wait(1) --Wait one second
button.BackgroundColor3 = Color3.fromRGB(85, 170, 0) --Set the color back to green
button.Text = "Buy" --Change the text to "Buy"
i
end
end
end)