Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i make a script that takes away points when an item is bought?

What i want to do is when the player has greater then or equal too (>=) points it subtracts points from the players in-game points. The thing was when the player didn't have enough money, it still subtracted it and gave them a negative number. Which i didn't want. And when i did less then or equal too (<=) when they had enough and they bought it and took away points it said they couldn't afford it when i didn't want it to.

This is my code:

(In a LocalScript)


local money = script.Parent.Parent.Parent.Parent.ShopMenu.PlayerPoints.num
local space1Avalible = script.Parent.Parent.PlanetSpotsAvalible.Ring_LVL1.Space1_Avalible
local space2Avalible = script.Parent.Parent.PlanetSpotsAvalible.Ring_LVL1.Space2_Avalible
local space3Avalible = script.Parent.Parent.PlanetSpotsAvalible.Ring_LVL1.Space3_Avalible
local space4Avalible = script.Parent.Parent.PlanetSpotsAvalible.Ring_LVL1.Space4_Avalible

local function addNewPlanetToList()
	local planetListItem = game.StarterGui.PlanetGui.ListOfPlanets.Planet1:Clone()
	planetListItem.Name = "Planet2"
	planetListItem.Text = "Planet2"
	planetListItem.Position = UDim2.new(0, 0, 0.35, 0)
end

local function getNewPlanet()
	local planet1 = workspace.PlanetPlacement
	local PlanetModel = Instance.new("Model")
	PlanetModel.Name = "Planet2"
	PlanetModel.Parent = workspace
	planet1.Parent = PlanetModel
	planet1.Position = workspace.PlanetPlacement2.Position
	addNewPlanetToList()
	print("Planet succesfully bought!")
end

script.Parent.MouseButton1Click:Connect(function()
	if money.Text >= "1500" then
		getNewPlanet()
		money.Text = money.Text - 1500
		script.Parent.Parent.Visible = false
		space2Avalible.Value = false
	end									--Right here!
end)

idk whats wrong..can someone plz tell me..?

3 comments

0

Try this on line 26 'if tonumber(money.Text) >= 1500 then'.

0

and on line 28 try 'money.Text = tostring(tonumber(money.Text) - 1500)'

0

@thesit123 that worked! Thank you!

1 answer

✓ Accepted answer

You're trying to subtract a number from a string, but you can't do that. That's like saying print("Hello" - 10). I suggest creating a NumberValue, inserting it into your player, then subtracting from that value. However, here is how you would do it your way.

-- Use WaitForChild() on your children. I'm not even going to attempt writing that out. 
local money = script.Parent.Parent.Parent.Parent.ShopMenu.PlayerPoints.num
script.Parent.MouseButton1Click:Connect(function()
	-- Grabs all string numbers in the text, then turns them into numbers. 
	local my_number = tonumber(string.match(money.Text, "%d+"))
	if my_number and my_number > 1500 then
		getNewPlanet()
        money.Text = my_number - 1500
        script.Parent.Parent.Visible = false
        space2Avalible.Value = false 
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.