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

How to make a sword giver check if the player already has a sword before giving them another one?

I have the following sabre giver script which gives a player a sabre when they click on a button.

local part = script.Parent; local clickDetector = part.ClickDetector;

function onMouseClick(player) game.Lighting["Sabre"]:Clone().Parent = player.Backpack; end

clickDetector.MouseClick:connect(onMouseClick);

However I also want it to check if the player already has a sabre and then doesn't give them another sabre if they already have one, so that players don't give themselves multiple weapons.

Is there a simple way to do this? I'm not to knowledgeable about scripting and I've had a few friends have a look at it, though none seem to have got it to work.

1 comment

0

I edit my answer, you should look at my new final product.

2 answers

✓ Accepted answer

We just check if something doesn't exist in the player's backpack.


#FindFirstChild FindFirstChild finds if there is a child there. If it is there is will return with the object, If not, it will return nil meaning nothing.

--Check if there is a humanoid
game:GetService("Players").PlayerAdded:connect(function(plyr)
	plyr.CharacterAdded:connect(char)
		if char:FindFirstChild("Humanoid") ~= nil then
			print("humanoid found")
		end
	end
end)

~= nil means not nil, if the function can't find the child then it returns nil, but since it can, it can print. Also, instead of seeing if it isn't nil you can see if it's true

game:GetService("Players").PlayerAdded:connect(function(plyr)
	plyr.CharacterAdded:connect(char)
		if char:FindFirstChild("Humanoid") then
			print("humanoid found")
		end
	end
end)

if char:FindFirstChild("Humanoid") then is the same as if char:FindFirstChild("Humanoid") == true then.



#Final Product

local part = script.Parent;
local clickDetector = part.ClickDetector;

function onMouseClick(player)
	if not player.Backpack:FindFirstChild("Sabre") and not player.Character:FindFirstChild("Sabre") then
		game.Lighting["Sabre"]:Clone().Parent = player.Backpack;
	end
end

clickDetector.MouseClick:connect(onMouseClick);



Hope it helps!





Tip: Make sure you're unable to drop the tool because people could drop the tool, click the part again, and then pick up the old one and he'll have 2 sabres.

3 comments

0

Thanks a lot it works perfectly well now! Your explanation is also really helpful, the script works exactly as you said it.

0

Lord, you forgot to add how if they player equips the weapon, they'll be able to get a second one

0

Oh nice catch.

No problem

local part = script.Parent; local clickDetector = part.ClickDetector --or whatever your part is

function onMouseClick(player) if player.Backpack:FindFirstChild("game.Lighting["Sabre"] then print("Player has") else  game.Lighting["Sabre"]:Clone().Parent = player.Backpack end end

clickDetector.MouseClick:connect(onMouseClick)

1 comment

0

Thanks for the swift reply, however it didn't seem to work. I assumed the additional quotation mark before game.Lighting was an error and didn't include it. When I tested the script with you code it still gave me multiple sabres and the message "Player has" did not appear in the output window.

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