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

How do I make a tool stop giving to players if they already have one?

Hey, I am trying to figure out how to get this tool to stop giving to players once they already have one so that they can' t have a lot. I am using this script to give the tool, how would I do this?

local debounce = false

function getPlayer(humanoid) 
local players = game.Players:children() 
for i = 1, #players do 
if players[i].Character.Humanoid == humanoid then return players[i] end 
end 
return nil 
end 

function onTouch(part) 

local human = part.Parent:findFirstChild("Humanoid") 
if (human ~= nil) and debounce == false then

debounce = true

local player = getPlayer(human) 

if (player == nil) then return end 

script.Parent:clone().Parent = player.Backpack

wait(2)
debounce = false
end
end


script.Parent.Parent.Touched:connect(onTouch) 

I am trying to make it so that it stops giving to players who have it, and keep giving to players that don't have it. Is this possible??

1 answer

✓ Accepted answer

I tried to make some edits to your script, for more details check below.

--Edited script, be sure to move this script out of the tool and move it into the block you are going to hit, also move the tool to the server storage.
--If you don't want to use these changes (^) then just add line 12 and 13 (in my script) to line 21 (in your script)

local debounce = false

script.Parent.Touched:Connect(function(part) --something touched the part
local human = part.Parent:FindFirstChild("Humanoid")  --tries to find humanoid through the block that touched the part
	if human ~= nil and debounce == false then --if debounce is false, and we find the humanoid
		debounce = true --turns on debounce
		local player = game.Players:GetPlayerFromCharacter(human.Parent) --finds the player through the humanoid's parent, which is the character
		if player ~= nil then --if we find the player
			local tool = player.Backpack:FindFirstChild("YourToolName") --trying to find if the player has the tool
			if tool == nil then --sees if the player does not have the tool
				game.ServerStorage.YourToolName:Clone().Parent = player.Backpack --gets the tool from server storage and gives it to the player's backpack
				wait(2)
				debounce = false --turns debounce back to false
			end
		end
	end
end)

3 comments

0

Thank you, works perfectly.

0

no problem! :)

0

Thank you and I fixed the problem about the shop

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