Hello, I’m new to scripting so I don’t really know how I can do this
I have this tool in my game that has a value named “Ammo” how can I add to that value and the same time remove a specified amount of money from the player?
Hello, I’m new to scripting so I don’t really know how I can do this
I have this tool in my game that has a value named “Ammo” how can I add to that value and the same time remove a specified amount of money from the player?
local Cost = 50
local AmmoAmount = 16
function onTouched(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Character = hit.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
if Player and Player.leaderstats.Money.Value >= Cost then
local Weapon = Player:FindFirstChild("Backpack"):FindFirstChild("LMG") or Character:FindFirstChild("LMG")
if Weapon then
Weapon.Ammo.Value += AmmoAmount
Player.leaderstats.Money.Value += -Cost
end
end
end
end
script.Parent.Touched:Connect(onTouched)
It’s kind of hard to get into detail, so I just made a simple script instead.
If the player has enough money, then we will add ammo and take money from the player.
local players = game:GetService("Players")
local ammoPart = script.Parent
local ammoPrice = 50
local ammoAmount = 16
local function onTouched(hit)
local hitModel = hit:FindFirstChildOfClass("Model")
if hitModel then
local hitPlayer = players:GetPlayerFromCharacter(hitModel)
if hitPlayer then
local hitBackpack = hitPlayer.Backpack
local hitWeapon = hitBackpack:FindFirstChild("LMG") or hitModel:FindFirstChild("LMG")
if hitWeapon then
if hitPlayer.leaderstats.Money.Value >= ammoPrice then
hitPlayer.leaderstats.Money.Value -= ammoPrice
hitWeapon.Ammo.Value += ammoAmount
end
end
end
end
end
ammoPart.Touched:Connect(onTouched)