How do I make this script always choose new tools

It only chooses a weapon once, and I want it to choose a random weapon whenever I trigger the proximity prompt. Please help!

--Module Script

local Tools = game.ReplicatedStorage.Tools
local Weapons = Tools:GetChildren()

local randomItem = Weapons[math.random(1, #Weapons)]
local randomClone = randomItem:Clone()
local randomItemChildren = randomClone:GetChildren()

local plr = game:GetService("Players").LocalPlayer

local chooseWeapon = {}

function chooseWeapon.choose()
	for _, commonR in pairs(randomItemChildren) do
		if commonR:IsA("StringValue") then
			if commonR.Value == "Common" then
				print("Common! :D")
				randomClone.Parent = game.StarterPack
			end
		end
	end
	for _, rareR in pairs(randomItemChildren) do
		if rareR:IsA("StringValue") then
			if rareR.Value == "Rare" then
				print("Rare")
				randomClone.Parent = game.StarterPack
			end
		end
	end
end

return chooseWeapon

--Server Script
local rWeapons = require(game.ServerScriptService.Modules.rWeapons)
local prox = game.Workspace:WaitForChild("WeaponHatchPart").ProximityPrompt

prox.Triggered:Connect(function()
	rWeapons.choose()
end)

Put the randomItem variable into the chooseWeapon.choose() function, in order for the math.random function to be ran mutliple times.

I also recommend changing game.StarterPack to game.Players.Backpack to give it to the player immediately, unless what you’re doing is intentional.

1 Like

like this?

local Tools = game.ReplicatedStorage.Tools
local Weapons = Tools:GetChildren()

local randomItem = Weapons[math.random(1, #Weapons)]
local randomClone = randomItem:Clone()
local randomItemChildren = randomClone:GetChildren()

local plr = game:GetService("Players").LocalPlayer

local chooseWeapon = {}

function chooseWeapon.choose(randomItem)
	for _, commonR in pairs(randomItemChildren) do
		if commonR:IsA("StringValue") then
			if commonR.Value == "Common" then
				print("Common! :D")
				randomClone.Parent = game.StarterPack
			end
		end
	end
	for _, rareR in pairs(randomItemChildren) do
		if rareR:IsA("StringValue") then
			if rareR.Value == "Rare" then
				print("Rare")
				randomClone.Parent = game.StarterPack
			end
		end
	end
end

return chooseWeapon

and I don’t want to change it to game.Players.Backpack

thats fine, but I don’t think you’ve understood me correctly.
What you need to do is move the local randomItem = Weapons[math.random(1, #Weapons)] line to the line just below function chooseWeapon.choose(randomItem)

and not pass the randomItem variable into the function

Sorry if i confused you!

1 Like

Could you edit the script for me? Because I don’t really know what you mean…

Sure! I’m confusing sometimes.


local Tools = game.ReplicatedStorage.Tools
local Weapons = Tools:GetChildren()

local plr = game:GetService("Players").LocalPlayer

local chooseWeapon = {}

function chooseWeapon.choose()
    local randomItem = Weapons[math.random(1, #Weapons)]
    local randomClone = randomItem:Clone()
    local randomItemChildren = randomClone:GetChildren()
	for _, commonR in pairs(randomItemChildren) do
		if commonR:IsA("StringValue") then
			if commonR.Value == "Common" then
				print("Common! :D")
				randomClone.Parent = game.StarterPack
			end
		end
	end
	for _, rareR in pairs(randomItemChildren) do
		if rareR:IsA("StringValue") then
			if rareR.Value == "Rare" then
				print("Rare")
				randomClone.Parent = game.StarterPack
			end
		end
	end
end

return chooseWeapon

Edit: didn’t realise the variables after the randomItem variable depended on it

1 Like

Yes, sorry I forgot to move the other 2 variables. Just copy and paste it again, I edited it to be fixed.

1 Like

OMG YOUR THE BEST!! But there’s also 1 problem, the tool doesn’t move to my inventory when I trigger the proximity prompt!!

That’s why I recommended moving the tool from StarterPack to the player’s Backpack. If you’d like, you can do the following, in order to make it go to both places:


local Tools = game.ReplicatedStorage.Tools
local Weapons = Tools:GetChildren()

local plr = game:GetService("Players").LocalPlayer

local chooseWeapon = {}

function chooseWeapon.choose()
    local randomItem = Weapons[math.random(1, #Weapons)]
    local randomClone = randomItem:Clone()
    local randomItemChildren = randomClone:GetChildren()
	for _, commonR in pairs(randomItemChildren) do
		if commonR:IsA("StringValue") then
			if commonR.Value == "Common" then
				print("Common! :D")
				randomClone.Parent = game.StarterPack
				local clone2 = randomClone:Clone()
				clone2.Parent = plr.Backpack
			end
		end
	end
	for _, rareR in pairs(randomItemChildren) do
		if rareR:IsA("StringValue") then
			if rareR.Value == "Rare" then
				print("Rare")
				randomClone.Parent = game.StarterPack
				local clone2 = randomClone:Clone()
				clone2.Parent = plr.Backpack
			end
		end
	end
end

return chooseWeapon
1 Like

image
thats why i used starterpack, because I run the function in a server script and It didn’t work when I used backpack

Right, thats because you can’t get the LocalPlayer in a server script

What you want to do is get the player from the proximity prompt triggered event and pass it on to the chooseweapon function. You can edit the server script to this:

local rWeapons = require(game.ServerScriptService.Modules.rWeapons)
local prox = game.Workspace:WaitForChild("WeaponHatchPart").ProximityPrompt

prox.Triggered:Connect(function(plr)
	rWeapons.choose(plr)
end)

and the module script to:


local Tools = game.ReplicatedStorage.Tools
local Weapons = Tools:GetChildren()

local chooseWeapon = {}

function chooseWeapon.choose(plr)
    local randomItem = Weapons[math.random(1, #Weapons)]
    local randomClone = randomItem:Clone()
    local randomItemChildren = randomClone:GetChildren()
	for _, commonR in pairs(randomItemChildren) do
		if commonR:IsA("StringValue") then
			if commonR.Value == "Common" then
				print("Common! :D")
				randomClone.Parent = game.StarterPack
				local clone2 = randomClone:Clone()
				clone2.Parent = plr.Backpack
			end
		end
	end
	for _, rareR in pairs(randomItemChildren) do
		if rareR:IsA("StringValue") then
			if rareR.Value == "Rare" then
				print("Rare")
				randomClone.Parent = game.StarterPack
				local clone2 = randomClone:Clone()
				clone2.Parent = plr.Backpack
			end
		end
	end
end

return chooseWeapon

hopefully this works? I don’t know how i managed to make the local players thing slip my mind

1 Like

It works! Thank you so much, you’re honestly the best! <3 Have a great day!!!

thank you! i hope you have a great day too.

1 Like

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