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

Cant get string.sub to get the value I need?

Hi im trying to make it so that when you say "/pay amounthere" a moneybrick spawns on you. I need help with the sub.string stuff because when I tried to print the variable that changes depending on what the amount is it doesn't print.

local player = game.Players.LocalPlayer
local limit = 10
local mon = 0
local playername = game.Players.LocalPlayer.Name
local drop = game.Workspace.playername.Torso.Position
local moneybrick = game.Lighting.Money:Clone()

function PayChatted(message, player)
	if string.sub(message, 1, 5) == "/pay " then
		mon = string.sub(message, 6)
		print(mon)
		if mon < limit then print("get richer")
		elseif mon > limit then
			player.leaderstats.Money = player.leaderstats.Money - mon
			moneybrick.money.Value = mon
			moneybrick.Parent = game.Workspace
			moneybrick.Position = drop
			end
		
	end
end
	
game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(message) PayChatted(message, player)
		end)
end)

1 answer

line 10 should be:

mon = tonumber(string.sub(message, 6, string.len(messsage))) or nil

and lines 12 through 19 should be replaced with:

if mon ~= nil then
	if mon < limit then
		print("get richer")
	elseif mon > limit then
		player.leaderstats.Money = player.leaderstats.Money - mon
		moneybrick.money.Value = mon
		moneybrick.Parent = game.Workspace
		moneybrick.Position = drop
	end
else
	warn("amount given is not a valid number")
end

1 comment

0

i hope you found this helpful!

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