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

Why doesnt this code work?

Asked by
Vividex 162
10 years ago
local sword = game.Lighting.LinkedSword

local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = script.Parent

ClickDetector.MouseClick:connect(function(player)
if not player.Backpack:FindFirstChild(sword.Name) and not (player.Character and player.Character:FindFirstChild(sword.Name)) then
sword:Clone().Parent = player.Backpack
end
end)

Its supposed to give the first person who clicks it the item "LinkedSword" from lighting, then it disables the brick so it doesnt give it to anyone else, then it checks the game to see if someone has it in their backpack, if no one has it in their backpack it enables the brick again for the other person. I mean it sort of works, but when I click it, it gives me the tool, and wont let me click it again, but it will give other players the tools even if someone has the item in their backpack. Can someone fix it so only one person gets the item then it disables the brick so no one else can get it but then if the player loses the item or does not have it, it enables itself again? I wanted to use boolean but wasn't sure how, thanks!

2 answers

Log in to vote
-1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago
local sword = game.Lighting.LinkedSword

local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = script.Parent

ClickDetector.MouseClick:connect(function(player)
    for i,v in pairs(game.Players:GetPlayers()) do --Loops through all players
        if v.Backpack:findFirstChild(sword.Name) or  --If the current player has it, then
        v.Character:findFirstChild(sword.Name) then 
            return --Stop the function
        end
    end
    sword:Clone().Parent = player.Backpack --Imaginary elseif, gives the player the tool if the function did not return. 
end)
0
It works, like when the person clicks it it wont allow anyone else click it, but when that person removes the item from their inventory, it just disables the whole script forever and wont let anyone get the sword if no one has it Vividex 162 — 10y
0
I edited it, read it now. Perci1 4988 — 10y
0
Thanks!:-) Vividex 162 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

I think you put a parenthesis by accident on (player.Character and

I am not the best scripter, it may need to go there, it may not.

local sword = game.Lighting.LinkedSword

local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = script.Parent

ClickDetector.MouseClick:connect(function(player)
if not player.Backpack:FindFirstChild(sword.Name) and not player.Character and player.Character:FindFirstChild(sword.Name)) then
sword:Clone().Parent = player.Backpack
end
end)

Answer this question