✓ Accepted answer
Indent in a more organized fashion! You'll be denoted as a more organized and professional scripter.
ToolName = "TMP"
RespawnTime = 6
Debounce = false
script.Parent.Touched:connect(function(hit)
if Debounce == false then
Debounce = true
p = game.Players:playerFromCharacter(hit.Parent)
if p == nil then
return
end
else
Wep = game.Lighting.TMP:Clone()
if p.Backpack:findFirstChild(Wep.Name) ~= nil then
return
end
else
Wep.Parent = p.Backpack
end
end
wait(RespawnTime)
Debounce = false
end
end)
Let's redo the script. The objective here is to, given the ToolName, clone the desired weapon and place it in the toucher's backpack - assuming that it is a player.
ToolName = "TMP"
RespawnTime = 6
Debounce = false
script.Parent.Touched:connect(function (Hit)
if not Debounce then
Debounce = true
wait(RespawnTime)
Debounce = false
end
end)
Let's wrap the "if" statement (with the Debounce as its condition) into another "if" statement that makes sure that the toucher is a) a valid character, b) is part of a player instance, and c) is (preferably) alive.
ToolName = "TMP"
RespawnTime = 6
Debounce = false
script.Parent.Touched:connect(function (Hit)
local HumanoidIsFound = Hit.Parent:FindFirstChild("Humanoid")
local PlayerIsFound = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Hit.Parent and HumanoidIsFound and HumanoidIsFound.Health > 0 and PlayerIsFound then
if not Debounce then
Debounce = true
local Weapon = game.ServerStorage:FindFirstChild(ToolName)
if Weapon then
local newWeapon = Weapon:Clone()
newWeapon.Parent = PlayerIsFound.Backpack
else
print("No such weapon named as " .. ToolName .. " in game.ServerStorage.")
Debounce = false
return
end
wait(RespawnTime)
Debounce = false
end
end
end)
Any questions? Comments? Feel free to ask.
2 comments
Can you add spacing please!
spacing where?