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)
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
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
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
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