Detect if returned loot from loot table is a string or number

Hi! I’m making a chest system, I have a modulescript that plays the animation for the chest, gets random loot from another module script that contains the loot table, But the thing is, I want the loot table to also have money or numbers, not just item names, I’ve tried detecting if the name of the returned loot is a number or string but I cant get it to work.

The main modulescript with getloot() function
local functions = {}
local loot = require(script.Loot)
local items = game.ServerStorage.Item	
local store = game.ReplicatedStorage.Store
local tweeninfo = TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
local ts = game:GetService("TweenService")

function getLoot()
	local sum = 0
	for i, chance in pairs(loot) do
		sum += chance
	end
	local random = math.random(sum)
	for i, chance in pairs(loot) do
		if random <= chance then
			if tostring(i) then
				return i
			elseif tonumber(i) then
				return 100000
			end
		else
			random -= chance
		end
	end
end

function functions.Get(plr, Chest, anim, controller)
	
	local track = controller:LoadAnimation(anim)
	local tween = ts:Create(Chest.PrimaryPart, tweeninfo, {CFrame = Chest.PrimaryPart.CFrame * CFrame.new(0, 5, 0)})
		
	for i, v in pairs(Chest.PrimaryPart:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			if v.Name == "Glow" then
				v.Enabled = true
			end
		end
	end
	
	tween:Play()
	tween.Completed:Wait()
	track:Play()
	
	track:GetMarkerReachedSignal("Open"):Connect(function()
		for i, v in pairs(Chest.PrimaryPart:GetChildren()) do
			if v:IsA("ParticleEmitter") then
				if v.Name == "Glow" then
					v.Enabled = false
				end
			end
		end
		for i, kk in pairs(Chest.PrimaryPart:GetChildren()) do
			if kk:IsA("ParticleEmitter") then
				if kk.Name == "Explode" then
					kk:Emit(kk:GetAttribute("EmitCount"))
				end
			end
		end
		
		if tostring(getLoot) then
			for i, k in pairs(items:GetDescendants()) do

				if k.Name == tostring(getLoot) then
					local item = k
					if item.Parent:IsA("Folder") then
						if item.Parent.Name == "Fruit" then
							local clone = item:Clone()
							clone.Parent = plr.Backpack
							local clone2 = store:Clone()
							clone2.Parent = clone
							print("plr got" .. item.Name .. "lol")
						elseif item.Parent.Name == "Weapons" then
							local clone = item:Clone()
							clone.Parent = plr.Backpack
							print("plr got" .. item.Name .. "lol")
						end
					end
				end
			end
		elseif tonumber(getLoot) then
			plr.Data.Beli.Value += getLoot
		end
		
		
		wait(2)
		track:Stop()
		Chest:Destroy()
	end)
	
end

return functions```
Loot Table
return {
	
	["Gomu"] = 10,
	["Bari"] = 10,
	["Toge"] = 30,
	["Suna"] = 3,
	["Pika"] = 3,
	["Magu"] = 3,
	["Mochi"] = 7,
	["Kiribachi"] = 20,
	["Bisento"] = 15,
	["Bara"] = 30,
	["Guru"] = 40,
	["Suke"] = 50,
	["100000"] = 30,


}

I would appreciate some help, thank you in advance! :blush:

You forgot to call the function, use: getLoot()

Also instead of tonumber(value) or tostring(value) use typeof(value)

Full edit:

local functions = {}
local loot = require(script.Loot)
local items = game.ServerStorage.Item	
local store = game.ReplicatedStorage.Store
local tweeninfo = TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
local ts = game:GetService("TweenService")

function getLoot()
	local sum = 0
	for i, chance in pairs(loot) do
		sum += chance
	end
	local random = math.random(sum)
	for i, chance in pairs(loot) do
		if random <= chance then
			if typeof(i) == “string” then
				return i
			elseif typeof(i) == “number” then
				return 100000
			end
		else
			random -= chance
		end
	end
end

function functions.Get(plr, Chest, anim, controller)
	
	local track = controller:LoadAnimation(anim)
	local tween = ts:Create(Chest.PrimaryPart, tweeninfo, {CFrame = Chest.PrimaryPart.CFrame * CFrame.new(0, 5, 0)})
		
	for i, v in pairs(Chest.PrimaryPart:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			if v.Name == "Glow" then
				v.Enabled = true
			end
		end
	end
	
	tween:Play()
	tween.Completed:Wait()
	track:Play()
	
	track:GetMarkerReachedSignal("Open"):Connect(function()
		for i, v in pairs(Chest.PrimaryPart:GetChildren()) do
			if v:IsA("ParticleEmitter") then
				if v.Name == "Glow" then
					v.Enabled = false
				end
			end
		end
		for i, kk in pairs(Chest.PrimaryPart:GetChildren()) do
			if kk:IsA("ParticleEmitter") then
				if kk.Name == "Explode" then
					kk:Emit(kk:GetAttribute("EmitCount"))
				end
			end
		end
		
        local resultLoot = getLoot()
		if type(resultLoot) == "string" then
			for i, k in pairs(items:GetDescendants()) do

				if k.Name:lower() == resultLoot:lower() then
					local item = k
					if item.Parent:IsA("Folder") then
						if item.Parent.Name == "Fruit" then
							local clone = item:Clone()
							clone.Parent = plr.Backpack
							local clone2 = store:Clone()
							clone2.Parent = clone
							print("plr got" .. item.Name .. "lol")
						elseif item.Parent.Name == "Weapons" then
							local clone = item:Clone()
							clone.Parent = plr.Backpack
							print("plr got" .. item.Name .. "lol")
						end
					end
				end
			end
		elseif type(resultLoot) == "number" then
			plr.Data.Beli.Value += resultLoot
		end
		
		
		wait(2)
		track:Stop()
		Chest:Destroy()
	end)
	
end

return functions
1 Like

I’d recommend using type() instead of typeof() for primitive value types as it operates faster & is hence more efficient.

you are using the wrong double quotes

"string"
"number"

Thanks! This does work, however sometimes it doesnt give me anything… and the tween isnt playing and moving the chest up. Also how would I make it so instead of it giving 100000, it gives the specific loot money that was returned from the loot table? for example it returned 35000 instead of 100000, so it’ll also give the player 35k

I’m not sure what you mean but I think the script is fixed, for example if the index is a number in a string it will convert the string to a number and return that number… I keep making some ridiculous mistakes because I’m on mobile and it’s late for me. The double quotes are messed up for me on mobile so if the leave any errors just replace them with new double quotes.

Script:

local functions = {}
local loot = require(script.Loot)
local items = game.ServerStorage.Item	
local store = game.ReplicatedStorage.Store
local tweeninfo = TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
local ts = game:GetService("TweenService")

function getLoot()
	local sum = 0
	for i, chance in pairs(loot) do
		sum += chance
	end
	local random = math.random(sum)
	for i, chance in pairs(loot) do
		if random <= chance then
			if tonumber(i) then
				return tonumber(i)
			elseif tostring(i) then
				return i
			end
		else
			random -= chance
		end
	end
end

function functions.Get(plr, Chest, anim, controller)
	
	local track = controller:LoadAnimation(anim)
	local tween = ts:Create(Chest.PrimaryPart, tweeninfo, {CFrame = Chest.PrimaryPart.CFrame * CFrame.new(0, 5, 0)})
		
	for i, v in pairs(Chest.PrimaryPart:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			if v.Name == "Glow" then
				v.Enabled = true
			end
		end
	end
	
	tween:Play()
	tween.Completed:Wait()
	track:Play()
	
	track:GetMarkerReachedSignal("Open"):Connect(function()
		for i, v in pairs(Chest.PrimaryPart:GetChildren()) do
			if v:IsA("ParticleEmitter") then
				if v.Name == "Glow" then
					v.Enabled = false
				end
			end
		end
		for i, kk in pairs(Chest.PrimaryPart:GetChildren()) do
			if kk:IsA("ParticleEmitter") then
				if kk.Name == "Explode" then
					kk:Emit(kk:GetAttribute("EmitCount"))
				end
			end
		end
		
        local resultLoot = getLoot()
		if type(resultLoot) == "string" then
			for i, k in pairs(items:GetDescendants()) do

				if k.Name:lower() == resultLoot:lower() then
					local item = k
					if item.Parent:IsA("Folder") then
						if item.Parent.Name == "Fruit" then
							local clone = item:Clone()
							clone.Parent = plr.Backpack
							local clone2 = store:Clone()
							clone2.Parent = clone
							print("plr got" .. item.Name .. "lol")
						elseif item.Parent.Name == "Weapons" then
							local clone = item:Clone()
							clone.Parent = plr.Backpack
							print("plr got" .. item.Name .. "lol")
						end
					end
				end
			end
		elseif type(resultLoot) == "number" then
			plr.Data.Beli.Value += resultLoot
		end
		
		
		wait(2)
		track:Stop()
		Chest:Destroy()
	end)
	
end

return functions
1 Like

I found a way through it, thanks.

1 Like

If I had a bigger screen on and it was earlier in the day, I probably would’ve been able to find the problem faster. But anyways, the only thing that matters is that you found your solution.

1 Like

Hey, sorry if this is a bit late but, this happens with the script u made for me that I’m using currently.


It just prints “Item” and then doesn’t destroy the chest but stop the track.
This happens randomly or if you open another chest after opening one.

local functions = {}
local gold = require(script.Gold)
local silver = require(script.Silver)
local items = game.ServerStorage.Item	
local store = game.ReplicatedStorage.Store
local tweeninfo = TweenInfo.new(0.25, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut)
local ts = game:GetService("TweenService")
local cam = game.ReplicatedStorage.RemoteEvents.Camera

function destroy(chest)
	for i, v in pairs(chest:GetChildren()) do
		if v:IsA("MeshPart") or v:IsA("Part") then
		-- local twin1 = ts:Create(v, tweeninfo, {Size = Vector3.new(0, 0, 0)})
			local twin = ts:Create(v, tweeninfo, {Transparency = 1})
		--	twin1:Play()
			twin:Play()
			twin.Completed:Connect(function()
				chest:Destroy()
			end)
		end
	end
	for i, kk in pairs(chest:WaitForChild("RootPart"):GetChildren()) do
		if kk:IsA("ParticleEmitter") then
			if kk.Name == "Explode" then
				kk:Emit(kk:GetAttribute("EmitCount") * 2)
			end
		end
	end
end

function getLoot(loottable)
	if loottable == "Gold" then
		local sum = 0
		for i, chance in pairs(gold) do
			sum += chance
		end
		local random = math.random(sum)
		for i, chance in pairs(gold) do
			if random <= chance then
				if type(i) == "string" then
					return i
				elseif type(i) == "number" then
					return i
				end
			else
				random -= chance
			end
			print(loottable)
		end
	elseif loottable == "Silver" then
		local sum = 0
		for i, chance in pairs(silver) do
			sum += chance
		end
		local random = math.random(sum)
		for i, chance in pairs(silver) do
			if random <= chance then
				if type(i) == "string" then
					return i
				elseif type(i) == "number" then
					return i
				end
			else
				random -= chance
			end
			print(loottable)
		end
	end
	
end

function functions.Get(plr, Chest, anim, controller, chesttype)

	local track = controller:LoadAnimation(anim)

	for i, v in pairs(Chest.PrimaryPart:GetChildren()) do
		if v:IsA("ParticleEmitter") then
			if v.Name == "Glow" then
				v.Enabled = true
			end
		end
	end
	
	cam:FireClient(plr, Chest.PrimaryPart.ChestPrompt, 2)
	track:Play()

	track:GetMarkerReachedSignal("Open"):Connect(function()
		for i, v in pairs(Chest.PrimaryPart:GetChildren()) do
			if v:IsA("ParticleEmitter") then
				if v.Name == "Glow" then
					v.Enabled = false
				end
			end
		end
		
		local resultLoot = getLoot(chesttype)
		if type(resultLoot) == "string" then
			for i, k in pairs(items:GetDescendants()) do
				if k.Name == resultLoot then
					local item = k
					if item.Parent:IsA("Folder") then
						if item.Parent.Name == "Fruit" then
							local clone = item:Clone()
							clone.Parent = plr.Backpack
							local clone2 = store:Clone()
							clone2.Parent = clone
							print("plr got" .. item.Name .. "lol")
							track:Stop()
							wait(0.5)
							destroy(Chest)
						elseif item.Parent.Name == "Weapons" then
							local clone = item:Clone()
							clone.Parent = plr.Backpack
							print("plr got" .. item.Name .. "lol")
							track:Stop()
							wait(0.5)
							destroy(Chest)
						end
					end
				end
			end
		elseif type(resultLoot) == "number" then
			plr.Data.Beli.Value += resultLoot
			track:Stop()
			print("Plr got money: " .. resultLoot)
			wait(0.5)
			destroy(Chest)
		end
		
	end)

end

return functions