Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why doesnt this code work?

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

✓ Accepted answer
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)

3 comments

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

0

I edited it, read it now.

0

Thanks!:-)

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.