The script below works when testing locally in studio, but fails to work when tested on a server. For the life of me I can't figure out why, I figured I'd ask here because it's probably something small that I overlooked.
sidenote: excuse all the prints, i tried using them to debug to no avail
local replicatedStorage = game:GetService("ReplicatedStorage")
local equipToolEvent = replicatedStorage:FindFirstChild("EquipToolEvent")
local player = game.Players.LocalPlayer
player:WaitForChild("Backpack")
local humanoid = game.Workspace:WaitForChild(player.Name):WaitForChild("Humanoid")
wait(2.5)
function getWeapons()
local backpack = player.Backpack
if backpack then
print("backpack")
local weapons = {}
for _, weapon in pairs(backpack:GetChildren()) do
print("getting " .. weapon.Name)
table.insert(weapons, weapon)
end
return weapons
else
print("!backpack")
end
end
function getBestWeapon(weapons)
if weapons then
print("weapons")
local bestWeapon
for _, weapon in pairs(weapons) do
print(weapon.Name)
if bestWeapon then
if weapon.Values.Damage.Value > bestWeapon.Values.Damage.Value then
bestWeapon = weapon
end
else
bestWeapon = weapon
end
end
return bestWeapon
else
print("!weapons")
end
end
local weapons = getWeapons()
local bestWeapon = getBestWeapon(weapons)
if bestWeapon then
equipToolEvent:FireServer(bestWeapon.Name)
else
print("!bestWeapon")
end

2 comments
Thanks for the answer, I tried your suggestion and although I too thought it made sense it unfortunately didn't seem to solve my problem.
hmm