This script gives a player a certain tool based on the number of an attribute inside a proximity prompt when it is triggered. The problem is, the script rarely works. There are no errors and nothing happens. Is there a flaw in the code that I am not seeing?
wait(5)
Map = game.Workspace.Maps[game.Workspace.Server.Map.Value]
for i,v in pairs(Map.Searchables:GetChildren()) do
v.Interact.Triggered:Connect(function(player)
print("Debug")
local Id = v.Interact:GetAttribute("LootId")
print("Debug")
if Id == 0 then
print("0")
player.PlayerGui.Remote:FireClient(player,3,"It's empty",3)
elseif Id == 1 then
print("1")
local tool = player.Character:FindFirstChildWhichIsA("Tool")
if tool then
tool.Handle.Equip.Enabled = true
wait()
tool.Handle.Name = "NotHandle"
tool.Parent = game.Workspace
end
local tool2 = game.Workspace.Server.Assets.Tools.Binoculars:Clone()
tool2.Parent = player.Character
player.PlayerGui.Remote:FireClient(player,3,"I found binoculars",3)
wait(0.1)
player.Character.Humanoid:EquipTool(tool2)
v.Interact:Destroy()
elseif Id == 2 then
print("2")
local tool = player.Character:FindFirstChildWhichIsA("Tool")
if tool then
tool.Handle.Equip.Enabled = true
wait()
tool.Handle.Name = "NotHandle"
tool.Parent = game.Workspace
end
local tool2 = game.Workspace.Server.Assets.Tools.LaserGun:Clone()
tool2.Parent = player.Character
player.PlayerGui.Remote:FireClient(player,3,"I found a laser gun",3)
wait(0.1)
player.Character.Humanoid:EquipTool(tool2)
v.Interact:Destroy()
end
end)
end
not 100% sure, but this might be your problem. it’s the same thing as adding a wait before a PlayerAdded() function, the script waits, and by that time the player has already joined the game.
there is another script that chooses a random map for my game but there is a delay of about 4 seconds when choosing it so if I didn’t put the wait(5), the script would return an error
local tool = player.Character:FindFirstChildWhichIsA("Tool")
if tool then
tool.Handle.Equip.Enabled = true
wait()
tool.Handle.Name = "NotHandle"
tool.Parent = game.Workspace
end
local tool2 = game.Workspace.Server.Assets.Tools.Binoculars:Clone()
tool2.Parent = player.Character
player.PlayerGui.Remote:FireClient(player,3,"I found binoculars",3)
wait(0.1)
player.Character.Humanoid:EquipTool(tool2)
v.Interact:Destroy()
as a function outside of the loop before it happens since you’re gonna be copying and pasting a lot the more ids you have, something like
local function pickUp(toollocation, tooltext)
local tool = player.Character:FindFirstChildWhichIsA("Tool")
if tool then
tool.Handle.Equip.Enabled = true
wait()
tool.Handle.Name = "NotHandle"
tool.Parent = game.Workspace
end
local tool2 = toollocation:Clone()
tool2.Parent = player.Character
player.PlayerGui.Remote:FireClient(player,3,tooltext,3)
wait(0.1)
player.Character.Humanoid:EquipTool(tool2)
v.Interact:Destroy()
end
Then your 2 statements for Id 1 and 2, just do
elseif Id == 1 then
print("1")
pickUp(game.Workspace.Server.Assets.Tools.Binoculars, "I found binoculars")
elseif Id == 2 then
print("2")
pickUp(game.Workspace.Server.Assets.Tools.LaserGun, "I found a laser gun")
end