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

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

Asked by 9 years ago

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.

0
I edit my answer, you should look at my new final product. EzraNehemiah_TF2 3552 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

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.

0
Thanks a lot it works perfectly well now! Your explanation is also really helpful, the script works exactly as you said it. Axmeister 10 — 9y
0
Lord, you forgot to add how if they player equips the weapon, they'll be able to get a second one alphawolvess 1784 — 9y
0
Oh nice catch. EzraNehemiah_TF2 3552 — 9y
Ad
Log in to vote
-2
Answered by
xuefei123 214 Moderation Voter
9 years ago

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)
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. Axmeister 10 — 9y

Answer this question