Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I can’t figure out how to remove this sword from my backpack?

Asked by 4 years ago
Edited by Ziffixture 4 years ago

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)

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
0
Yes, I recently made a thread regarding one of my items not getting out of ServerStorage to Backpack, but then I got told it had to come from ReplicatedStorage(it would replicate to all devices). 6zk8 95 — 4y
0
oh ok thank you! geyygiraffe -1 — 4y
Ad

Answer this question