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 = Playerprint(“Loaded Leaderstats”)
local Coins = Instance.new(“IntValue”)
Coins.Name = “Coins”
Coins.Parent = leaderstatsprint(“Loaded Coins”)
local Level = Instance.new(“IntValue”)
Level.Name = “Level”
Level.Parent = leaderstatsprint(“Loaded Level”)
local PlayerUserId = “Player_”…Player.UserId
print(“Loaded Player Id”)
local CoinValueUI = game.StarterGui.HUD.CoinCount.CoinValue
local LevelValueUI = game.StarterGui.HUD.LevelCountCoinValueUI.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 thenCoins.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.UserIdlocal 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)
StartGui Explorer
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!