Script only works sometimes

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
1 Like

Which part of the script only works sometimes or is it all of the script that works sometimes?

The entire script does not work

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.

To add onto @dude7271, why are you even waiting 5 seconds anyways?

1 Like

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

Can’t you do

local Map = game.Workspace.Maps:WaitForChild(game.Workspace.Server.Map.Value)

to remedy the wait?

that’s bad practice, try adding some sort of remote function to trigger the script once the map is loaded

Ok thanks guys for your help. I’ll add these in the script and see if it works

1 Like

try what @EmbatTheHybrid said first

I also recommend adding

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
1 Like

Even better he could use a module script

1 Like

Thank you guys! The script works perfectly now

1 Like