Script not working

Ive created this script but it doesnt work can someone help me fix it

local toolName = "RedKey"
local targetPart = script.Parent

local function onTouch(part)
	local character = part.Parent
	local player = game.Players:GetPlayerFromCharacter(character)

	if player then
		local tool = player.Backpack:FindFirstChild(toolName)

		if tool then
			local handle = tool:FindFirstChild("Handle")
			if handle and part == targetPart then
				targetPart.Anchored = false
				targetPart.CanCollide = false
			end
		end
	end
end

targetPart.Touched:Connect(onTouch)

1 Like

try “if handle and targetPart == part then”, might help

1 Like
local toolName = "RedKey"
local targetPart = script.Parent

local function onTouch(part)
	local character = part.Parent
	local player = game.Players:GetPlayerFromCharacter(character)

	if player then
		local tool = player.Backpack:FindFirstChild(toolName)

		if tool then
			local handle = tool:FindFirstChild("Handle")
			if handle and targetPart == part then
				targetPart.Anchored = false
				targetPart.CanCollide = false
			end
		end
	end
end

targetPart.Touched:Connect(onTouch)

Like this if yes then it doesnt work

Print character and player in the onTouch() function, if its nil then there’s a problem.

Also can you tell us the context of this script? Why is there a part parented to the player’s character?

are there any errors? This could help us figure out what’s going wrong

This script makes it so when a player with a “RedKey” touches a special part, the part changes (like becoming movable or not colliding with other things).

The part is probably something the player needs to interact with or use, like a key or switch, and it’s attached to the player so they can control it.

local toolName = "RedKey"
local targetPart = script.Parent

local function onTouch(part)
	local character = part.Parent
	local player = game.Players:GetPlayerFromCharacter(character)

	print("Character:", character)
	print("Player:", player)

	if player then
		local tool = player.Backpack:FindFirstChild(toolName)

		if tool then
			local handle = tool:FindFirstChild("Handle")
			if handle and part == targetPart then
				targetPart.Anchored = false
				targetPart.CanCollide = false
			end
		end
	end
end

targetPart.Touched:Connect(onTouch)

and on the player it did printed nil

i have no idea theres no errors in output about it

maybe we could check if the touching part belongs to a character before accessing the player.

local toolName = "RedKey"
local targetPart = script.Parent

local function onTouch(part)
	local character = part.Parent
    if character:FindFirstChild("Humanoid") then
	local player = game.Players:GetPlayerFromCharacter(character)

	print("Character:", character)
	print("Player:", player)
		local tool = player.Backpack:FindFirstChild(toolName)

		if tool then
			local handle = tool:FindFirstChild("Handle")
			if handle and part == targetPart then
				targetPart.Anchored = false
				targetPart.CanCollide = false
			end
	end
end
end

targetPart.Touched:Connect(onTouch)

Well now its doesnt show nil in the output it shows the naem of the player

that’s great, that means that the scripts know which player it’s working with. If the script is still broken, you could probably add more print checkpoints to find the issue