In the tycoon I'm working on, players create resources to sell to other players, so I've made buttons for purchasing them, however every attempt to make the scripts work the way I want has ended poorly. I'm not asking for someone to do it for me, but can someone point me in the right direction as to how to do it?

repeat wait() until script.Parent.Product.Value~=nil
local product=script.Parent.Product.Value:Clone()
script.Parent.Product.Value:remove()

local price=script.Parent.Price.Value

script.Parent.Name=script.Parent.ProductName.Value.." - "..price --sets name of brick
if price==0 then
	script.Parent.Name=script.Parent.ProductName.Value.." - FREE"
end

script.Parent.Head.Touched:connect(function(hit) --this is how people buy it
	chr=hit.Parent
		plr=game.Players:GetPlayerFromCharacter(chr)
		if chr:findFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(chr) and plr.leaderstats.Money.Value>=price then
		if plr.Name==script.Parent.Parent.Parent.OwnerName.Value then
			plr.leaderstats.Money.Value=plr.leaderstats.Money.Value-price
			product.Parent=script.Parent.Parent.Parent
			script.Parent:remove()
		end
	end
end)

1 comment

0

There is a easier way to check if the object that triggered the function belongs to the Player or not. Simply Change line 15 to "if plr then" .Since in line 14 the script checks if the part that triggered the function is controlled by a Player if it is then it will return the Player's name and if it isn't it will return nil