So im making a little sword arena in my obby game and i scripted a brick to give you a sword and when you walk back outside the arena a door takes ur sword away. I put this script inside the door but whenever I playtest it and walk through it does not work. Can you help me?
script.Parent.Touched:Connect(function(hit) local player = game.Players:FindFirstChild(hit.Parent) local sword = game.Lighting:FindFirstChild("ClassicSword") local findSword = player.Backpack:FindFirstChild("ClassicSword") if findSword then sword.Parent = game.Lighting end if not findSword then print("Player does not have a sword in their Backpack!") warn() end end)
Hit.Parent
presumably correlates to the Character if touched by a Player. You tried to search the Players
container for a Character Rig, this won’t locate the Player, and will return nil. You can easily fix this by using the GetPlayerFromCharacter
method to find them based off of their Rig.
It is recognized as a poor practice to keep Instances stored within Lighting
, I suggest you move them to ReplicatedStorage
instead.
local ReplicatedStorage = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") local Part = script.Parent Part.Touched:Connect(function(hit) local Player = Players:GetPlayerFromCharacter(hit.Parent) if (Player and Player.Backpack) then local FoundSword = Player.Backpack:FindFirstChild("ClassicSword") if (FoundSword) then FoundSword.Parent = ReplicatedStorage else warn(Player.Name.."’s Sword is missing") end end end)