I was trying to make a simple script so when a player touches a part they are given a tool. Everything worked well except that a single player could get tons of tools.
So what you want is that a player doesnât getâs tool twice?
Provide code please
If you donât want the player to get lots of tools a lot of times in such speed then you could use debounces
but if you donât want the player to get the tool twice you can verify if the player already has it in his backpack
yes sorry for not posting code here it is:
local taco = game.ReplicatedStorage.Taco
script.Parent.Touched:Connect(function(plr)
local hum = plr.Parent:FindFirstChildWhichIsA(âHumanoidâ)
if hum then
local tacoclone = taco:Clone()
tacoclone.Parent = hum.Parent
end
end)
local taco = game.ReplicatedStorage.Taco
script.Parent.Touched:Connect(function(hit)
local p = game.Players:GetPlayerFromCharacter(hit.Parent)
if p then
if (p.Backpack:FindFirstChild('Taco')) then
return
end
local tacoclone = taco:Clone()
tacoclone.Parent = p.Backpack
end
end)
Gope it helps!
tried it and got an error [ Workspace.TacoStand.TacoTouch.TacoTouch:6: Expected â)â (to close â(â at column 4), got âthenâ]
local taco = game.ReplicatedStorage.Taco
script.Parent.Touched:Connect(function(plr)
local hum = plr.Parent:FindFirstChildWhichIsA(âHumanoidâ)
if hum then
local LocalPlayer = game.Players:GetPlayerFromCharacter(plr.Parent)
if LocalPlayer ~= nil then
if not LocalPlayer.Backpack:FindFirstChild("Taco") or not plr.Parent:FindFirstChild("Taco") then
local tacoclone = taco:Clone()
tacoclone.Parent = hum.Parent
end
end
end
end)
this did not work and gave the exact same results as the script I had before
Try this;
local taco = game.ReplicatedStorage.Taco
script.Parent.Touched:Connect(function(hit)
local p = game.Players:GetPlayerFromCharacter(hit.Parent)
if p then
if (p.Backpack:FindFirstChild('Taco')) then
return
end
local tacoclone = taco:Clone()
tacoclone.Parent = p.Backpack
end
end)
This almost worked but it gave me two tacos which was weird
local taco = game.ReplicatedStorage.Taco
script.Parent.Touched:Connect(function(plr)
local hum = plr.Parent:FindFirstChildWhichIsA("Humanoid")
if hum then
local LocalPlayer = game.Players:GetPlayerFromCharacter(plr.Parent)
if LocalPlayer ~= nil then
if not LocalPlayer.Backpack:FindFirstChild("Taco") then
if not plr.Parent:FindFirstChild("Taco") then
local tacoclone = taco:Clone()
tacoclone.Parent = hum.Parent
end
end
end
end
end)
Jusy for reference when you encounter multiple result on the connect event next time, you might need to use debounce