1.When I click on part and I have 20 gold, they don’t give me anything and the script doesn’t work correctly, help me solve this problem.
2. How can I make the script work once, because when I stand on the part it tries to endlessly take 20 gold from me.
The script:
1 Like
Hello, Before finding a player from the character you should always find the humanoid of the character first.
For the other question you should put a debounce if you don’t want the part to endlessly call the function.
Maybe This Should Help:
currency = "Gold"
amnt = 20
hold = false
area = script.Parent
plrs = game:GetService("Players")
Sound2 = area.Sound2
Sound3 = area.Sound3
function GiveProduct(human) -- use this if you want to give a product after purchase if not, delete it.
local player = plrs:GetPlayerFromCharacter(human)
local newshirt = game.ServerStorage.Tool:Clone()
newshirt.Parent = player.Backpack
end
function onTouched(part)
local h = part.Parent:findFirstChild("Humanoid")
if (h~=nil and hold==false) then
hold=true
local plyr = game.Players:findFirstChild(part.Parent.Name)
if plyr ~= nil then
local player = plrs:GetPlayerFromCharacter(part.Parent)
if player ~= nil then
local cash = player.leaderstats:findFirstChild(currency)
if cash ~= nil then
if cash.Value >= amnt then --has enough currency, the player can purchase
cash.Value = cash.Value - amnt
Sound3:Play()
GiveProduct(part.Parent)
elseif cash.Value <= amnt then -- doesn't have enough currency.
Sound2:Play()
end
end
end
wait(3)
end
hold=false
end
end
area.Touched:connect(onTouched)