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.
We just check if something doesn't exist in the player's backpack.
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
.
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.
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)