Talk:Money/Shop Scripts

From Roblox Wiki

Jump to: navigation, search

This was quick and it is unfinished, ill add more later.

Ideas for new sections: Getting weps(lighting hack)

And.. well.. thats all I have. Feel free to post some ideas!

---PCwiener 17:23, 26 August 2007 (CDT)

nice PC you taught me something!


--Sloso 07:28, 27 August 2007 (CDT)

playerFromCharacter

Instead of doing this: game.Players:findFirstChild(hit.Parent.Name), try doing this: game.Players:playerFromCharacter(hit.Parent)

---Dingdong272

Try using Debounce, it will stop multiple touches and then people's money won't get taken away as fast.

How about a Buy/Sell Tool -- A suggestion for a new page.

Before I looked at this tutorial, I was experimenting with making a tool for buying and selling items. I add new things to this script, but this is what I have so far. You can put this in a hopper bin to buy or sell items by clicking on them, but you have to set up an item a special way.

--Objects have to be set up a special way to work.
--The message that appears when using this tool is meant to be like talking to the store owner.
--Variables with [*] can be changed. Get creative with the name of your land's money!


money = script.Parent.Parent.Parent.leaderstats.Coins -- Where the money value is for you character. This is added with a separate script.

moneyName = "Coins" --[*] This is what the currency of your land is called.

MaxDistance = 20 --[*] How far away you can be and still buy the item. This way, you can't buy an item from the other side of the map.

ItemSpot = script.Parent.Parent -- [*] Since this tool is set up for tools, this is where the item goes. 
--If you're buying/selling something else, you might have to change some things around.

--DO NOT CHANGE THESE
object = "Nothing" -- This is the actual item's reference.
character = script.Parent.Parent.Parent.Character
bartering = false
ref = "Nothing" -- These are values for the item. This is where all the values are.
msg = nil

function onButton1Down(mouse)
	local obj = mouse.Target
	if obj ~= nil then
		selling = obj:findFirstChild("Selling")
		buying = obj:findFirstChild("BuyPrice")
		local torso = script.Parent.Parent.Parent.Character.Torso
		if (obj.Position - torso.Position).magnitude <= MaxDistance then
			if selling ~= nil and buying ~= nil then
				ref = obj
				object = obj.Item.Value
				item = object
				msg.Text = "You may buy ('b') that for "..selling.Value.." "..moneyName..", or I will buy ('r') one of that item for "..buying.Value.." "..moneyName.."."
			elseif selling == nil and buying ~= nil then
				ref = obj
				object = obj.Item.Value
				item = object
				msg.Text = "You can't buy that, but you can sell ('r') it to me for "..buying.Value.." "..moneyName.." if you have any of it."
			elseif selling ~= nil and buying == nil then
				ref = obj
				object = obj.Item.Value
				item = object
				msg.Text = "I won't buy it back, but I'll sell ('b') that to you for "..selling.Value.." "..moneyName.."."
			else
				print("That's not for sale!")
				object = "Nothing"
				msg.Text = "Click on an object for sale to begin bartering."
			end
		elseif (obj.Position - torso.Position).magnitude > MaxDistance and selling == nil and buying == nil then
			print("That's not for sale!")
			object = "Nothing"
			msg.Text = "Click on an object for sale to begin bartering."
		else
			print("You are too far!")
			object = "Nothing"
			msg.Text = "You are to far away to buy that"
			wait(2)
			msg.Text = "Click on an object for sale to begin bartering."
		end
	else
		print("Click on something")
		msg.Text = "Click on an object for sale to begin bartering."
	end
end

function sell()
	if buying ~= nil then
		local findTool = ItemSpot:findFirstChild(item.Name)
			if findTool ~= nil then
				print("Tool found")
				local sellPrice = buying
				if sellPrice ~= nil then
					local bulk = findTool:findFirstChild("Amount")
					if bulk ~= nil and bulk.Value > 0 then
						bulk.Value = bulk.Value - 1
						money.Value = money.Value + sellPrice.Value
						if bulk.Value > 0 then
							msg.Text = "Thank you. You have "..bulk.Value.." of that item left."
						else
							msg.Text = "You have none of that item remaining."
						end
					elseif bulk ~= nil and bulk.Value == 0 then
						msg.Text = "You have none of that item remaining."
					elseif bulk == nil then
						money.Value = money.Value + sellPrice.Value
						findTool:remove()
						msg.Text = "Thank you for your patronage."
					end
				end
			else
				msg.Text = "You don't have anything to sell." -- You don't have any of the item for sale.
			end
	else
		msg.Text = "I'm sorry, but I can't buy that item from you. You should probably keep it." -- The stor owner won't buy it.
	end
end

function buy()
	if selling ~= nil then
		if money.Value - selling.Value >= 0 then
			local getHasObj = ItemSpot:findFirstChild(item.Name)
			if getHasObj == nil then
				item:clone().Parent = ItemSpot
				money.Value = money.Value - selling.Value
			elseif getHasObj ~= nil then
				print("The player has that item, but can still buy more")
				local objBulk = getHasObj:findFirstChild("Amount")
				local itemBulk = ref:findFirstChild("Amount")
				if objBulk ~= nil and itemBulk ~= nil then
					money.Value = money.Value - selling.Value
					objBulk.Value = objBulk.Value + itemBulk.Value
					msg.Text = "You bought "..itemBulk.Value.." more of that item."
				else
					msg.Text = "You've got that item already, and I'm not sellng it in bulk amounts."
				end
			end
		else
			msg.Text = "You don't have enough money! Get out of my store!" -- You made the store owner angry because you don't have enough money.
		end
	else
		msg.Text = "Didn't I say that's not for sale?" -- You can sell the store owner this item, but it's not for sale.
	end
end

function onSelected(mouse)
	msg = Instance.new("Hint")
	msg.Parent = script.Parent.Parent.Parent
	msg.Text = "Click on an object for sale to begin bartering."
	
	mouse.KeyDown:connect(onKeyDown)
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
	
	hum = script.Parent.Parent.Parent.Character:findFirstChild("Humanoid")
	if hum ~= nil then
		connection = hum.Died:connect(RemoveMessageIfDead) -- This way, if the player dies, the message doesn't get stuck.
	end
end

function onDeselected(mouse)
	msg:remove()
	if hum ~= nil then
		connection:disconnect()
	end
end

function RemoveMessageIfDead() -- Doesn't it make you angry when a message gets stuck and you can't remove it?
	if msg ~= nil then -- This will fix that problem.
		msg:remove()
	end
end

function onKeyDown(key) -- How to buy or sell with the keyboard.
	key = key:lower()
	if key == "b" then
		if object ~= "Nothing" then
			buy()
		end
	end
	if key == "r" then
		if object ~= "Nothing" then
			sell()
		end
	end
end
	
script.Parent.Selected:connect(onSelected)
script.Parent.Deselected:connect(onDeselected)

This can be used to buy or sell items with the mouse.

Here's a diagram of how to set up an object for sale. These objects (except for the brick) must be named exactly as they are here, or the script above will misinterpret the object.

Brick - The brick (or part) that is clicked on to buy an object.

Amount** - The amount sold of the object if it is in bulk amounts. By bulk amounts, I mean like in stacks of 10 or 20.
BuyPrice* - How much the store owner will buy this for if they are buying it.
Item - This is an object value. It defines where the item that the player gets is.
ItemScript - This is a one line script that defines the value of Item. ex: script.Parent.Item.Value = game.Lighting.Item
Selling* - The price of the item for sale.

[*]These are IntValues, and you can have one, the other, or both, but if you have neither, the object will just be an ordinary brick.

[**]There is a special way to set up bulk items.

For an item the is a bulk item, you must have the IntValue "Amount" in the brick, and also in the tool that is put in the player's backpack or wherever you clone it to.
For an item that is not a bulk item, just take the amount variable out of the brick and the actual item that is put in the player's backpack or wherever you clone it to.


I probably have a few errors in that script, so just let me know and I will fix the error. I know that what I have put together right now is a bit vague, but if the moderators approve of it I can put together another page about this tool and merge it with this page. -Malvaviscos 12:11, 31 December 2008 (UTC)

Personal tools