Shop Script to Remove 'X' Amount Currency and Give Player Temporary Ability

As the title suggests, I am making an Ability Shop. When the player presses a purchase button, a set number of value of the players Coins is taken away and in my case, the players walkspeed increases 50%.

ServerScriptService > DataStore

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(Player)

local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = Player

print(“Loaded Leaderstats”)

local Coins = Instance.new(“IntValue”)
Coins.Name = “Coins”
Coins.Parent = leaderstats

print(“Loaded Coins”)

local Level = Instance.new(“IntValue”)
Level.Name = “Level”
Level.Parent = leaderstats

print(“Loaded Level”)

local PlayerUserId = “Player_”…Player.UserId

print(“Loaded Player Id”)

local CoinValueUI = game.StarterGui.HUD.CoinCount.CoinValue
local LevelValueUI = game.StarterGui.HUD.LevelCount

CoinValueUI.Text = Coins.Value
LevelValueUI.Text = "Level "…Level.Value

– Load Data

local data

local success, errormessage = pcall(function()
data = myDataStore:GetAsync(PlayerUserId)
end)

if success then
if data then

  	Coins.Value = data.Coins
  	Level.Value = data.Level
  	
  end
  
  print("Loaded Data")
  
  -- Set our data equal to current Coins

end

end)

  -- Saving data while player is leaving a game

game.Players.PlayerRemoving:Connect(function(Player)
local PlayerUserId = “Player_”…Player.UserId

local data = {
Coins = Player.leaderstats.Coins.Value;
Level = Player.leaderstats.Level.Value;
}

local success, errormessage = pcall(function()
myDataStore:SetAsync(PlayerUserId, data)
end)

if success then
print(“Data successfully saved!”)
else
print(“Error!”)
end
end)

ServerScriptService > PurchaseHandler

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

local Items = game.StarterGui.Shops.Powerups.Shop.Items
local Price = Items.Price.Value
local ItemName = Items.ItemName.Value

– Speed

game.Players.PlayerAdded:Connect(function(Player)

local leaderstats = Player:FindFirstChild(“leaderstats”)

local Coins = leaderstats.Coins

local Button = Items.Parent.Info.PurchaseButton

Button.MouseButton1Up:Connect(function() – Open Shop

  Coins.Value -= Price
  
  local function Ability()
  	
  	game.StarterPlayer.CharacterWalkSpeed = 24
  	
  	wait(120)
  	
  end

end)

end)

The Players Explorer (in game)
image

StartGui Explorer
image

For background context I’m working on a solo project to practice my programming and this is my most recent of many walls I’ve smacked into. I tried making the code as easy to understand for myself as possible. Any way to solve my issue and optimise my code is thoroughly appreciated!

Button.MouseButton1Up:Connect(function()
if Coins.Value >= Price then
Coins.Value -= Price
Ability()
return "Ability bought"
elseif Coins.Value < Price then
return "Not enough coins"
end
end)

Also try using Button.Activated instead of MouseButton1Up so mobile players can press it also

Note that this would not work as you put a return before calling Ability(), you need to swap their positions.


sorry i kinda just copy and pasted your code in