How can i use variables outside its scope

local MarketplaceService = game:GetService("MarketplaceService")
local SSS = game:GetService("ServerScriptService")
local API = require(game.ServerScriptService.Items)
local player = script.Parent.Parent.Parent.Parent.Parent.Parent
local bp = player.Backpack


local totalval = 1337
function GetItemDetails()
	local success, item, rap, value, defaultValue
		= API.fetchItemDetails(thisitemID)
	if success then
		print(defaultValue)
		thisvalue = defaultValue * 1
	end
end


function DGTYPE1()
	
	thisitem = script.Parent.Parent.Parent.Parent.Parent.Parent.Backpack:FindFirstChildWhichIsA("Tool")
	
	thisitemID = thisitem.Name

	GetItemDetails()

	mainItemValue = thisvalue * math.random(100, 117) / 100
	mainItemValue2 = thisvalue * math.random(100, 114) / 100
    mainItemValue3 = thisvalue * math.random(100, 117) / 100
	mainItemValue4 = thisvalue * math.random(100, 113) / 100

	minValue = math.round(mainItemValue / (math.random(405, 450) / 100)) 
	maxValue = math.round(mainItemValue / (math.random(360, 400) / 100))

	minValue2 = math.round(mainItemValue2 / (math.random(405, 450) / 100)) 
	maxValue2 = math.round(mainItemValue2 / (math.random(360, 400) / 100))

	minValue3 = math.round(mainItemValue3 / (math.random(405, 450) / 100)) 
	maxValue3 = math.round(mainItemValue3 / (math.random(360, 400) / 100))

	minValue4 = math.round(mainItemValue4 / (math.random(405, 450) / 100)) 
	maxValue4 = math.round(mainItemValue4 / (math.random(360, 400) / 100))

	
    success, item1 = API.findItemByValueRange(minValue, maxValue)
    success, item2 = API.findItemByValueRange(minValue2, maxValue2)
    success, item3 = API.findItemByValueRange(minValue3, maxValue3)
    success, item4 = API.findItemByValueRange(minValue4, maxValue4)


	script.Parent.item1.Image = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. thisitem.Name .. "&width=420&height=420&format=png"
	
	script.Parent.Parent.YourUpg.item1.Image = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. item1 .. "&width=420&height=420&format=png"
	
	script.Parent.Parent.YourUpg.item2.Image = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. item2 .. "&width=420&height=420&format=png"
	
	script.Parent.Parent.YourUpg.item3.Image = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. item3 .. "&width=420&height=420&format=png"
	
	script.Parent.Parent.YourUpg.item4.Image = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. item4 .. "&width=420&height=420&format=png"
	

	function GetItemDetails1()
		local success, item, rap, value, defaultValue
			= API.fetchItemDetails(item1)
		if success then
			print(defaultValue)
			xval1 = defaultValue 
		end
	end
	wait()
	function GetItemDetails2()
		local success, item, rap, value, defaultValue
			= API.fetchItemDetails(item2)
		if success then
			print(defaultValue)
			xval2 = defaultValue 
		end
	end
	wait()
	function GetItemDetails3()
		local success, item, rap, value, defaultValue
			= API.fetchItemDetails(item3)
		if success then
			print(defaultValue)
			xval3 = defaultValue 
		end
	end
	wait()
	function GetItemDetails4()
		local success, item, rap, value, defaultValue
			= API.fetchItemDetails(item4)
		if success then
			print(defaultValue)
			xval4 = defaultValue 
		end
	end
	
	GetItemDetails1()
	wait()
	
	GetItemDetails2()
	wait()
	GetItemDetails3()
	wait()
	GetItemDetails4()
	wait()
	
	totalval = (xval1 + xval2 + xval3 + xval4)
	
	script.Parent.Parent.BotRBX.Text = "R$ " .. totalval
	script.Parent.Parent.YourRBX.Text = "R$ " .. thisvalue
	
end
wait()


script.Parent.Parent.TextButton.MouseButton1Click:Connect(function()
	print("xd")
	local Clone1 = Instance.new("Tool")
	Clone1.Name = item1
	Clone1.TextureId = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. item1 .. "&width=420&height=420&format=png"
	Clone1.Parent = player.Backpack

	local Clone2 = Instance.new("Tool")
	Clone2.Name = item2
	Clone2.TextureId = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. item2 .. "&width=420&height=420&format=png"
	Clone2.Parent = player.Backpack

	local Clone3 = Instance.new("Tool")
	Clone3.Name = item3
	Clone3.TextureId = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. item3 .. "&width=420&height=420&format=png"
	Clone3.Parent = player.Backpack

	local Clone4 = Instance.new("Tool")
	Clone4.Name = item4
	Clone4.TextureId = "https://www.roblox.com/asset-thumbnail/image?assetId=" .. item4 .. "&width=420&height=420&format=png"
	Clone4.Parent = player.Backpack
end)


while true do
	DGTYPE1()
	wait(5)
end

i need to use the item1, item2, item3 and item4 variables outside the function

how can i do that

1 Like
  1. Declare the variables outside the function, and grab the data inside the function
local i1, i2, i3, i4

local function ex()
    i1 = 1
    i2 = 2
    i3 = 3
    i4 = 4
end
  1. return the data using return
local function ex()
    return 1, 2, 3, 4
end

local i1, i2, i3, i4 = ex()
-- assigns variables with returned data accrodingly

return essentially pulls data from the function while ending it aswell.

2 Likes

OH MY GOD THANK YOU YOU’RE THE BEST I BEEN TRYING FOR 5 HOURS

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.