Need help on proximity prompt and tool

Hi, so this code won’t work when I equip a tool, and use the proximity prompt, it doesn’t do the thing it’s supposed to do. I think there’s a problem with the “local ToolEquipped = Player.Character and Player.Character:FindFirstChildOfClass(“Tool”)” however I’m not sure, as you already saw the script is running in LocalScript, and the scripts parent is a Proximity Prompt.

script.Parent.Triggered:Connect(function()
	local Player = game.Players.LocalPlayer
	local ToolEquipped = Player.Character and Player.Character:FindFirstChildOfClass("Tool")

	if ToolEquipped then
		local toolName = ToolEquipped.Name

		if toolName == "Cake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cake Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Cookie Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cookie Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Cupcake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cupcake Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Mochi Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Mochi Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Muffin Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Muffin Dough"]:Clone()
			Clone.Parent = Player.Backpack
		end
	end
end)

4 Likes

LocalScripts will not work in the Workspace. Replace the current script with an ServerScript instead.

Add a parameter for the Player when the ProximityPrompt gets triggered. This will directly point to the Player who triggered the ProximityPrompt.

script.Parent.Triggered:Connect(function(player)
-- code here
1 Like

Doesn’t work,

script.Parent.Triggered:Connect(function(Player)
	local ToolEquipped = Player.Character and Player.Character:FindFirstChildOfClass("Tool")

	if ToolEquipped then
		local toolName = ToolEquipped.Name

		if toolName == "Cake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cake Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Cookie Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cookie Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Cupcake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cupcake Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Mochi Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Mochi Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Muffin Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Muffin Dough"]:Clone()
			Clone.Parent = Player.Backpack
		end
	end
end)```

I added the script into a Serverside one, not a client one.
3 Likes

Remove Player.Character in the code, and keep only Player.Character:FindFirstChildOfClass(“Tool”) for the ToolEquipped variable.

Player.Character is simply pointing to the Player’s Character, which isn’t a tool itself, and would not make the script work as intended.

2 Likes

Makes an error of Workspace.Dif.ProximityPrompt.Script:3: attempt to index nil with ‘Character’ :sob:

1 Like

Wait, my bad I forgot to remove something. Ignore what I said earlier.

1 Like

Still doesn’t work. It doesn’t error anything, weird.

script.Parent.Triggered:Connect(function(Player)
	local ToolEquipped = Player.Character:FindFirstChildOfClass("Tool")

	if ToolEquipped then
		local toolName = ToolEquipped.Name

		if toolName == "Cake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cake Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Cookie Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cookie Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Cupcake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cupcake Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Mochi Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Mochi Dough"]:Clone()
			Clone.Parent = Player.Backpack

		elseif toolName == "Muffin Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Muffin Dough"]:Clone()
			Clone.Parent = Player.Backpack
		end
	end
end)
2 Likes

If you think that part might be the issue, then try using the debugger and Watch window to inspect variables and control flow as the program executes one line at a time.

1 Like

It’s probably with the if toolEquipped then etc, I tried this script below but it only printed working once:

script.Parent.Triggered:Connect(function(Player)
	local ToolEquipped = Player.Character:FindFirstChildOfClass("Tool")
	print("working!")

	if ToolEquipped then
		local toolName = ToolEquipped.Name
		print("working!")

		if toolName == "Cake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cake Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")

		elseif toolName == "Cookie Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cookie Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")

		elseif toolName == "Cupcake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cupcake Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")

		elseif toolName == "Mochi Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Mochi Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")

		elseif toolName == "Muffin Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Muffin Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")
		end
	end
end)
3 Likes

I don’t seem to see anything wrong with the script. Could you send a screenshot of where the Script is located in the game’s Explorer?

1 Like

image

Here.

2 Likes

Like I said before, you need to change this to a ServerScript instead of a LocalScript.

LocalScripts don’t work in the Workspace, so keep that in mind.

1 Like

Do I use the same script?

script.Parent.Triggered:Connect(function(Player)
	local ToolEquipped = Player.Character:FindFirstChildOfClass("Tool")
	print("working!")

	if ToolEquipped then
		local toolName = ToolEquipped.Name
		print("working!")

		if toolName == "Cake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cake Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")

		elseif toolName == "Cookie Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cookie Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")

		elseif toolName == "Cupcake Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Cupcake Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")

		elseif toolName == "Mochi Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Mochi Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")

		elseif toolName == "Muffin Dough" then
			ToolEquipped:Destroy()
			local Clone = game.ServerStorage["Rolled Muffin Dough"]:Clone()
			Clone.Parent = Player.Backpack
			print("working!")
		end
	end
end)
2 Likes

Yep, it will work in the same way!

1 Like

Still doesn’t work, it just prints 1 “working” instead of printing multiple ones.

1 Like

Hm, how about trying to replace FindFirstChildOfClass() with FindFirstChildWhichIsA()?

1 Like

Nope still the same like before.

1 Like

Okay, let’s try to debug from the name of the ToolEquipped variable.

Can you temporarily remove the code below the first print() function, and just detect if a Player is holding a tool, and try printing the results. Something like:

script.Parent.Triggered:Connect(function(Player)
	local ToolEquipped = Player.Character:FindFirstChildOfClass(“Tool”)
	print(ToolEquipped.Name)
end)
2 Likes

Workspace.Dif.ProximityPrompt.Script:3: attempt to index nil with ‘Name’, thats the error. I also have the dough equipped.

1 Like

Try to replace:

print(ToolEquipped.Name)

With:

print(ToolEquipped)

See if it changes anything.

1 Like