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)
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)
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)
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)
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