Preventing the player from picking a tool up if the player has an objectvalue

I am making a script that prevents the player from picking a tool up if they have an objectvalue. I made a while true do loop that deletes touchinterest so the tool does not automatically pick up. The TouchedEvent fires perfectly fine but the tool does not parent to the player’s backpack whether they have the objectvalue or not.

Code:

local function checkfortouchinterest()

while true do
	wait()
		if script.Parent:FindFirstChild("TouchInterest") then
		script.Parent:FindFirstChild("TouchInterest"):Destroy()
	end
end
end
spawn(checkfortouchinterest)



script.Parent.Touched:Connect(function(Hit)
	print("triggered touched event")
		if Hit and Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
			
		local player = players:GetPlayerFromCharacter(Hit.Parent)
			print("revolver has been touched")
		
		if not player:FindFirstChild("murderervalue") then
			
			local rev  = script.Parent.Parent
			
			rev.Parent = player.Backpack
			
			if script.Parent.Anchored == true then
				script.Parent.Anchored = false
			end
		
			if script.Parent.Parent.Parent == Hit.Parent then
			script.Parent.Parent.Parent = player.Backpack
		end
			
				if script.Parent.Sparkles.Enabled == true then
					script.Parent.Sparkles.Enabled = false
			end
		end
end
end)

You can’t prevent players from picking up a tool if it has a part named Handle.

Solution: Rename the handle to something else and make a custom pickup script that parents it to their character or their Backpack appropriately.

You could reparent the tool to the workspace upon it being picked up (or backpack upon it being equipped).

local run = game:GetService("RunService")
local players = game:GetService("Players")
local tool = script.Parent
local handle = tool.Handle

tool.AncestryChanged:Connect(function(_, parent)
	if parent.ClassName == "Model" then
		local player = players:GetPlayerFromCharacter(parent)
		if player then
			local bool = player.Value
			if bool.Value then
				handle.Position = parent.PrimaryPart.CFrame.lookVector * 5
				run.Stepped:Wait()
				tool.Parent = workspace
			end
		end
	end
end)

This works in respect to the value of a “BoolValue” instance placed inside the client’s player instance. Here’s the script which creates and sets the value of the “BoolValue” instance and parents it to each player.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local bool = Instance.new("BoolValue")
	bool.Value = true
	bool.Parent = player
end)

Here’s the model file if you’d like to inspect further.
repro.rbxm (3.6 KB)

https://gyazo.com/b9c61150591151391ae5a545c32dab1c

local run = game:GetService("RunService")
local players = game:GetService("Players")
local tool = script.Parent
local handle = tool.Handle

tool.Equipped:Connect(function()
	local character = tool.Parent
	local player = players:GetPlayerFromCharacter(character)
	if player then
		local bool = player.Value
		if bool.Value then
			run.Stepped:Wait()
			tool.Parent = player.Backpack
		end
	end
end)

Here’s a similar version which puts the tool back in the backpack upon it being equipped by the player’s character.

https://gyazo.com/749d49088187cb1dbdddde80fe850b6a

I appreciate the help but I have not made myself clear and I do apologize. I am making a murder mystery game and basically the object value is inserted into the killer so I am trying to make it so whoever has the object value is unable to pickup the tool (the gun).

The issue here is that you are basically going back and forth between your TouchInterest destroy code and your Touched connection. Connecting Touched on a BasePart creates a TouchInterest inside that part. This is how it can detect if someone is touching the part or not. So whenever you are connecting Touched, you are automatically destroying it because of your while loop.

And what I said applies perfectly to what you’re asking. The solution is to rename the handle to something else like FakeHandle and use a script such as this one to allow picking up the tool. (place the script inside the FakeHandle)

--!strict

local players = game:GetService('Players')
local handle = script.Parent
local tool = handle.Parent

if handle.Name == 'Handle' or script:FindFirstAncestorOfClass('Player') or players:GetPlayerFromCharacter(tool:FindFirstAncestorOfClass('Model')) then
	script.Parent.Name = 'Handle'
	script:Destroy()
end

local triggered = false
local conn

conn = handle.Touched:Connect(function(part)
	if triggered then return end

	local character = part:FindFirstAncestorOfClass('Model')
	local player = if character then players:GetPlayerFromCharacter(character) else nil
	local backpack = if player then player:FindFirstChildOfClass('Backpack') else nil

	if character and player and backpack then
		local holding = character:FindFirstChildWhichIsA('Tool')

		-- Remove tool from workspace so that the renaming to Handle doesn't allow someone else to pick it up
		tool.Parent = backpack

		-- Unequip current tool
		if holding then holding.Parent = backpack end

		-- Equip this tool
		handle.Name = 'Handle'
		tool.Parent = character

		script:Destroy()
		triggered = true
		conn:Disconnect()
	end
end)

You can modify this code to check the character for your BoolValue. In my case I had to use a version of this script to make it so that tools you pick up don’t override whatever you are currently holding. The one here should replicate the default Roblox behavior.

Ah, thank you this has solved my problem. Tried to rename the handle but wrote the script very differently.

1 Like