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

Why I can't destroy the Sword that's on the Players Backpack?

Asked by
Borrahh 265 Moderation Voter
4 years ago

So basically, the Sword is not being destroyed from player's I don't get any kind of errors, anyone knows what am I doing wrong?

local playersPlaying = require(game.ReplicatedStorage.playersPlaying)
local Sword = game.ReplicatedStorage.Tools.Sword:Clone()


script.Parent.MouseButton1Click:Connect(function()
    script.Parent.Parent.Parent.Visible = false

    for i, v in ipairs(playersPlaying) do
        local didPlayerUseSword = Instance.new("BoolValue")
        didPlayerUseSword.Name = "didPlayerUseSword"
        didPlayerUseSword.Value = false
        didPlayerUseSword.Parent = v
    end

    for i, v in ipairs(playersPlaying) do
        Sword.Parent = v.Backpack
        v.Backpack.Sword.Activated:Connect(function()
            v.didPlayerUseSword.Value = true
        end)
    end


    wait(5)
    script.Parent.Parent.Parent.Visible = true

    for i, v in pairs(playersPlaying) do
        --KILL PLAYER
        if v.didPlayerUseSword.Value == false then
            v.Character.Humanoid.Health = 0
            table.remove(playersPlaying, i)
        end
            print(v.Name.." Clicked The Tool")
            for i, v in pairs(playersPlaying) do
            local sword = v.Backpack:FindFirstChild("Sword")
            if sword then
            sword:Destroy()
            end
        end 
    end
end)

2 answers

Log in to vote
2
Answered by 4 years ago

Try checking for the sword on both the character and the backpack.

if v.Backpack:FindFirstChild("Sword") or v.Character:FindFirstChild("Sword") then
0
The Sword was located on the character, It was silly of me, I didn't think about it, Thanks! Borrahh 265 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

In your first line, you are using require, which loads a moduleScript. You are trying to make a list of activePlayers. This can be done with:

game.Players:GetChildren()

or by inserting a bool value when players join

game.Players.PlayerAdded:Connect(function(plr)
     local tag = Instance.new("BoolValue",plr)
     tag.Name = "Playing?"
     tag.Value = true
end)

(If this method is used, you'll need to find the value.)

for i,v in pairs(game.Players:GetChildren()) do
     if v:FindFirstChild("Playing?") then
          if v["Playing?"].Value == true then
               --Code here
          end
     end
end

You should also use plr.Backpack:FindFirstChild("Sword") to make sure that the player has a sword. (Of course, the plr part will be whatever you have your player labeled as.)

Other than that, your script is correct.

Answer this question