So im encountering this issue on my script (Mostly helped by an ai). for the most part it works. Only issue is it seems to take out of everyone’s coins. I can’t seem to put my finger on why it’s doing that, i’m new to scripting.
local CurrencyDataStore = DataStoreService:GetDataStore("CurrencyDataStore")
local toolToClone = game.ReplicatedStorage.Cola
local function deductCoins(player, amount)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local coins = leaderstats:FindFirstChild("Coins")
if coins and coins:IsA("NumberValue") then
if coins.Value >= amount then
coins.Value = coins.Value - amount
local success, result = pcall(function()
local key = tostring(player.UserId)
CurrencyDataStore:SetAsync(key, coins.Value)
end)
if not success then
warn("Failed to save coins to DataStore: " .. tostring(result))
end
local thanksSound = Instance.new("Sound")
thanksSound.SoundId = "rbxassetid://2609873966"
thanksSound.Volume = 1
thanksSound.RollOffMaxDistance = 30
thanksSound.RollOffMinDistance = 10
thanksSound.Parent = script.Parent
thanksSound:Play()
print("Deducted coins successfully.")
return true
else
local notEnoughSound = Instance.new("Sound")
notEnoughSound.SoundId = "rbxassetid://9066167010"
notEnoughSound.Volume = 1
notEnoughSound.RollOffMaxDistance = 30
notEnoughSound.RollOffMinDistance = 10
notEnoughSound.Parent = script.Parent
notEnoughSound:Play()
print("Not enough coins.")
return false
end
else
print("Coins NumberValue not found.")
end
else
print("Leaderstats not found.")
end
return false
end
local function cloneToolAndDeductCoins(player)
if deductCoins(player, 5) then
local clonedTool = toolToClone:Clone()
clonedTool.Parent = player.Backpack
print("Tool cloned and coins deducted.")
else
print("Not enough coins.")
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local clickDetector = script.Parent.ClickDetector
clickDetector.MouseClick:Connect(function()
cloneToolAndDeductCoins(player)
end)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player:WaitForChild("PlayerGui")
local key = tostring(player.UserId)
local success, result = pcall(function()
return CurrencyDataStore:GetAsync(key)
end)
local coins = Instance.new("NumberValue")
coins.Name = "Coins"
coins.Value = success and result or 100
coins.Parent = leaderstats
end)
I’ve tried getting the character from the click detector but it doesn’t seem to work. i need assistance