-------------------------------------------------------------------------------------------------------------------------


upgradeStuff = model:clone()--The thing that will be spawned when button is pushed
										--basically the "After"-part of the upgrade.. :P 

model:remove() --remove the ingame model. We saved a clone for later.
owner = script.Parent.Parent.Parent.OwnerName 
local ting = 0 --debouncer

function onTouched(hit)
	if ting == 0 then --check for debounce
	ting = 1 --activate debounce 
	check = hit.Parent:FindFirstChild("Humanoid") --try to find the human that touched the button

	if check ~= nil then --if toucher is a human then to following
	if hit.Parent.Name == owner.Value then
		local user = game.Players:GetPlayerFromCharacter(hit.Parent) --get player from touching human
		local stats = user:findFirstChild("leaderstats") --Find moneyholder

		if stats ~= nil then --If moneyholder exists then
			local cash = stats:findFirstChild("Money") --Get money
			if cash.Value > (Upgradecost-1) then --If there is enough money then

				cash.Value = cash.Value - Upgradecost --remove cash
				upgradeStuff.Parent = script.Parent.Parent.Parent --put the upgrade into the game
				script.Parent.Parent:remove() -- remove the upgrade button (in case of one-upgrade only)

			end
		end
	end
	end

	ting = 0 --remove debounce
	end
end

script.Parent.Touched:connect(onTouched) --start checking for touchers
