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

How can I stop someone from receiving an item from a giver if they already have that item?

Asked by
jacobwow 140
9 years ago

This script gives a player an item from ServerStorage called StrikeBall. However, I can not prevent a player from receiving multiple StrikeBalls. This is my attempt:

local Giver = script.Parent
local Gear = game.ServerStorage.StrikeBall
local debounce = false

function onTouch(part)
    local Player = part.Parent:findFirstChild("Humanoid")
    if Player ~= nil then
        if debounce == false then
            local Location = game:GetService('Players'):GetPlayerFromCharacter(Player.Parent)
            if Location.TeamColor == Giver.BrickColor then
        if Location.Character:FindFirstChild("StrikeBall") then
            print("StrikeBall found in inventory.")
    elseif Location.Backpack:FindFirstChild("StrikeBall") then
            print("StrikeBall found in backpack.")
        else debounce = true
        local New = Gear:clone()
        New.Parent = Location.Backpack
        wait (5)
        debounce = false
                end
            end
        end
    end
Giver.Touched:connect(onTouch)

2 answers

Log in to vote
0
Answered by 9 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

I made a helper method to to check and see if the person who touched the giver has the tool or not

local Giver = script.Parent
local Gear = game.ServerStorage.StrikeBall
local debounce = false

local function FindTool(player, tool)
    local Character = player.Character
    local HoldingTool = Character:FindFirstChild(tool)
    local BackpackTool = player.Backpack:FindFirstChild(tool)

    if HoldingTool ~= nil or BackpackTool ~= nil then
        return true
    end
end

Giver.Touched:connect(function(part) 
    debounce = true
    local Player  = game.Players:GetPlayerFromCharacter(part.Parent)

    if Player ~= nil and Player.TeamColor == Giver.BrickColor and debounce and Player:FindTool("StrickBall") ~= nil then
        Gear:Clone().Parent = Player.Backpack
    end

    wait(5)
    debounce = false
end)
0
Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer. AmericanStripes 610 — 9y
0
I understand that, but I am at a loss for words when trying to explain this. DewnOracle 115 — 9y
Ad
Log in to vote
-1
Answered by
Wizzy011 245 Moderation Voter
9 years ago

Just put :FindFirstChild("StrikeBall") ~= nil then

You were really close!

0
A Lua if check will pass as long as the condition is not nil or false. In other words, if something exists, the check will pass DewnOracle 115 — 9y

Answer this question