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

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

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

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

2 comments

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.

0

I understand that, but I am at a loss for words when trying to explain this.

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

You were really close!

1 comment

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

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