Universal random giver

Hello
I have problems with random giver.
Error

Workspace.SCP.Folder.Tool giver [Proximity Prompt].ProxPart.ProximityPrompt.Script:35: attempt to call a string value - Server - Script:35

is it this line

       local Tool = Storage:FindFirstChild(ToolName(nahoda))

??? !!!

print(nahoda)

is it for control random number for me, its OK. Everytime, when I press button, genered numbers between 1 to 16.

local ToolName1 = {"AKM"} 
local ToolName2 = {"AR2"} 
local ToolName3 = {"Barret"} 
local ToolName4 = {"CM4A1"} 
local ToolName5 = {"Dixie"} 
local ToolName6 = {"FN-FAL"} 
local ToolName7 = {"Ithaca37"} 
local ToolName8 = {"Keycard Level 1"} 
local ToolName9 = {"Keycard Level 2"} 
local ToolName10 = {"Keycard Level 3"} 
local ToolName11 = {"Keycard Level 4"} 
local ToolName12 = {"Keycard Level 5"} 
local ToolName13 = {"Keycard Level 6"} 
local ToolName14 = {"Keycard Level 7"} 
local ToolName15 = {"Keycard Level 8"} 
local ToolName16 = {"Keycard Level 9"}


local Storage = game.ServerStorage.Tools 

local Settings = script.Settings
local Cas1 = Settings["Cas1"].Value
local Cas2 = Settings["Cas2"].Value

local Part = script.Parent.Parent
local ProximityPrompt = script.Parent

ToolName = {}

ProximityPrompt.Triggered:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		local nahoda = math.random(1,16)
		print(nahoda)
		local Tool = Storage:FindFirstChild(ToolName(nahoda))
				if Tool then
				Tool:clone().Parent = Backpack					
				end
				
			
		
	end
	Part.ProximityPrompt.Enabled = false
	Part.CanCollide = false
	Part.Transparency = 1.0
	Part.CanTouch = false
	wait(math.random(Cas1, Cas2))
	Part.CanCollide = true
	Part.Transparency = 0.0
	Part.CanTouch = true
	Part.ProximityPrompt.Enabled = true	
end)

Thanks before
Codycheck

1 Like

ToolName is being defined as a table on line 28.

Try using square brackets to index the table, parentheses indicate a function call (ToolName[nahoda]).

Thank for reaction
I try change

local Tool = Storage:FindFirstChild(ToolName(nahoda))

for

local Tool = Storage:FindFirstChild(ToolName[nahoda])

and output error

Argument 1 missing or nil - Server - Script:35

Vrrrrrr :slight_smile:

Oh, that’s because there table is empty. You have to put some strings inside of the table.

No.
It must be simple
I have variables ToolName1, ToolName2, ToolName3 …etc
I generate numbers for the variable “ToolNameTHISGENERATENUMBERS”

ToolName(…) or ToolName[…] script does not accept

when is wrong

Codycheck

HEUREKA, but very long code :slight_smile:

It can be simplified

local ToolName1 = "AKM" 
local ToolName2 = "AR2" 
local ToolName3 = "Barret" 
local ToolName4 = "CM4A1"
local ToolName5 = "Dixie"
local ToolName6 = "FN-FAL" 
local ToolName7 = "Ithaca37" 
local ToolName8 = "Keycard Level 1" 
local ToolName9 = "Keycard Level 2" 
local ToolName10 = "Keycard Level 3" 
local ToolName11 = "Keycard Level 4" 
local ToolName12 = "Keycard Level 5" 
local ToolName13 = "Keycard Level 6" 
local ToolName14 = "Keycard Level 7" 
local ToolName15 = "Keycard Level 8" 
local ToolName16 = "Keycard Level 9"

local nastroj -- nastroj is word in czech language for tool

local Storage = game.ServerStorage.Tools 

local Settings = script.Settings
local Cas1 = Settings["Cas1"].Value
local Cas2 = Settings["Cas2"].Value

local Part = script.Parent.Parent
local ProximityPrompt = script.Parent

ProximityPrompt.Triggered:connect(function(Player)
	if Player and Player.Character then
		local Backpack = Player:WaitForChild("Backpack")
		-- here generate random numbers
		local nahoda = math.random(1,12)  --nahoda is word in czech language for random
		-- simplify this code
		if nahoda == 1 then
			nastroj = ToolName1
		elseif nahoda == 2 then
			nastroj = ToolName2
		elseif nahoda == 3 then
			nastroj = ToolName3
		elseif nahoda == 4 then
			nastroj = ToolName4
		elseif nahoda == 5 then
			nastroj = ToolName5
		elseif nahoda == 6 then
			nastroj = ToolName6
		elseif nahoda == 7 then
			nastroj = ToolName7
		elseif nahoda == 8 then
			nastroj = ToolName8
		elseif nahoda == 9 then
			nastroj = ToolName9
		elseif nahoda == 10 then
			nastroj = ToolName10
		elseif nahoda == 11 then
			nastroj = ToolName11
		elseif nahoda == 12 then
			nastroj = ToolName12
		elseif nahoda == 13 then
			nastroj = ToolName13
		elseif nahoda == 14 then
			nastroj = ToolName14
		elseif nahoda == 15 then
			nastroj = ToolName15
		elseif nahoda == 16 then
			nastroj = ToolName16
		end
		--control generate number and variables
		print(nahoda)
		print(nastroj)
		
		local Tool = Storage:FindFirstChild(nastroj)
				if Tool then
				Tool:clone().Parent = Backpack					
				end
		
			
		
	end
-- hide giver and setup time
	Part.ProximityPrompt.Enabled = false
	Part.CanCollide = false
	Part.Transparency = 1.0
	Part.CanTouch = false
	wait(math.random(Cas1, Cas2)) -- Cas is word czech language for time
	Part.CanCollide = true
	Part.Transparency = 0.0
	Part.CanTouch = true
	Part.ProximityPrompt.Enabled = true	
end)

Thanks before
Codycheck
[/quote]

local Server = game:GetService("ServerStorage")
local Tools = Server.Tools

local Settings = script.Settings
local Prompt = script.Parent
local Part = Prompt.Parent

local ToolNames = {"AKM", "AR2", "Barret", "CM4A1", "Dixie", "FN-FAL", "Ithaca37"}
for i = 1, 9 do
	table.insert(ToolNames, "Keycard Level "..i)
end

Prompt.Triggered:Connect(function(Player)
	local Backpack = Player:WaitForChild("Backpack")

	local Tool = Tools:FindFirstChild(ToolNames[math.random(#ToolNames)])
	if Tool then
		local ToolClone = Tool:Clone()
		ToolClone.Parent = Backpack		
	end
	
	Prompt.Enabled = false
	Part.Transparency = 1
	Part.CanCollide = false
	Part.CanTouch = false
	task.wait(math.random(Settings["Cas1"].Value, Settings["Cas2"].Value))
	Prompt.Enabled = true
	Part.Transparency = 0
	Part.CanCollide = true
	Part.CanTouch = true
end)

Thanks for help me

Now I’m just choosing whether the tool is already in the backpack
Maybe see you soon :slight_smile: :slight_smile:

Hello, here’s a different version which will prevent duplicate tools from being awarded.

local Server = game:GetService("ServerStorage")
local Tools = Server.Tools

local Settings = script.Settings
local Prompt = script.Parent
local Part = Prompt.Parent

local ToolNames = {"AKM", "AR2", "Barret", "CM4A1", "Dixie", "FN-FAL", "Ithaca37"}
for i = 1, 9 do
	table.insert(ToolNames, "Keycard Level "..i)
end

local function GiveRandomTool(Character, Backpack)
	local Tool = Tools:FindFirstChild(ToolNames[math.random(#ToolNames)])
	if Tool then
		if Character:FindFirstChild(Tool.Name) then
			GiveRandomTool(Character, Backpack)
		else
			local ToolClone = Tool:Clone()
			ToolClone.Parent = Backpack
		end
	end
end

Prompt.Triggered:Connect(function(Player)
	local Character = Player.Character
	local Backpack = Player:WaitForChild("Backpack")

	GiveRandomTool(Character, Backpack)

	Prompt.Enabled = false
	Part.Transparency = 1
	Part.CanCollide = false
	Part.CanTouch = false
	task.wait(math.random(Settings["Cas1"].Value, Settings["Cas2"].Value))
	Prompt.Enabled = true
	Part.Transparency = 0
	Part.CanCollide = true
	Part.CanTouch = true
end)
1 Like

Ah okay, I see the issue. I would approach this by using a table:

local toolNames = {
ToolName1 = "AKM";
ToolName2 = "AR2" ;
ToolName3 = "Barret"; 
ToolName4 = "CM4A1";
ToolName5 = "Dixie";
ToolName6 = "FN-FAL"; 
ToolName7 = "Ithaca37"; 
ToolName8 = "Keycard Level 1"; 
ToolName9 = "Keycard Level 2" ;
ToolName10 = "Keycard Level 3"; 
ToolName11 = "Keycard Level 4" ;
ToolName12 = "Keycard Level 5" ;
ToolName13 = "Keycard Level 6" ;
ToolName14 = "Keycard Level 7" ;
ToolName15 = "Keycard Level 8" ;
ToolName16 = "Keycard Level 9";
}

local nastroj -- nastroj is word in czech language for tool

local Storage = game.ServerStorage.Tools 

local Settings = script.Settings
local Cas1 = Settings["Cas1"].Value
local Cas2 = Settings["Cas2"].Value

local Part = script.Parent.Parent
local ProximityPrompt = script.Parent

ProximityPrompt.Triggered:connect(function(Player)
	if Player and Player.Character then
		local backpack = Player:WaitForChild('Backpack')
        local nahoda = math.random(1,16)
        
        local tool = storage:FindFirstChild(toolNames['ToolName'..nahoda])
        if tool then
        -- rest of func here

I recommend separating all of those objects in a folder. So it can be called easier and it looks nicer as well.

local RS = game:GetService('ReplicatedStorage')

local Keycards = RS.Keycards:GetChildren()

print(Keycards[1]) --Method 1
print(Keycards['Keycard Level 1']) --Method 2

Remember, children in a folder are seperated in alphabetical order so call them based on their order for method 1.