here is the code:
local sound = script.Parent.Money local cost = 0 script.Parent.Touched:Connect(function(hit) if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then if _G.Money >= cost then sound:Play() Instance:Remove(script.Parent) end end end)
heres the money variable:
local Players = game:GetService("Players") local function leaderboardSetup(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Money = Instance.new("IntValue") Money.Name = "Money" Money.Value = 0 Money.Parent = leaderstats end -- Connect the "leaderboardSetup()" function to the "PlayerAdded" event Players.PlayerAdded:Connect(leaderboardSetup)
I do not recommend using _G
because hackers might easily access the variable and get infinite money. Instead, get money from the leaderstats. The first script should be like this:
local sound = script.Parent.Money local cost = 0 script.Parent.Touched:Connect(function(hit) if hit and hit.Parent and hit.Parent:FindFirstChildOfClass("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then local player = game.players:GetPlayerFromCharacter(hit.Parent) local leaderstats = player:FindFirstChild(“leaderstats”) if leaderstats then local MoneyValue = leaderstats:FindFirstChild(“Money”) if MoneyValue then if MoneyValue.Value >= cost then MoneyValue.Value = MoneyValue.Value - cost sound:Play() script.Parent:Destroy() end end end end end)