Alright so first off, use variables. You typed that big GetPlayerFromCharacter line three times.
Keep your variable styles consistent -- capitalized, or not capitalized. You have Player, but then you also have hit and debounce.
Combine if statements with and.
Use the second argument of Instance.new to shorten your code.
Wait until everything is finished until enabling the script.
Don't put the space between the else and the if. What that's doing is basically just the same thing as what else does -- except with an if statement shoved inside. You can't have mutiple else's and you need an extra end. It's just a bad habit to get into.
So with these basic problems fixed (although it still won't work, because we haven't addressed the main issue yet), the script should look something like this:
local debounce = false
function onTouch(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and player.TeamColor == script.Parent.BrickColor and not debounce then
debounce = true
Instance.new("ForceField", player.Character)
debounce = false
end
end
Now for the main problem.
if Player.Backpack.Gun == Player.Backpack.Gun then
Alright, this just makes no sense. Of course the gun will equal the gun. This is one of the basic laws of logic. My cat isn't a parachute. My computer isn't a pizza. Something is what it is. A equals A.
But what if the gun is nil? Then you have a problem. You just indexed a nil value, and the code will error. You need to use FindFirstChild. Since it works like a function, instead of throwing an error if the child doesn't exist, it just returns nil, so it's useful in situations like these. Just keep in mind that it's only really useful in if statements.
local debounce = false
function onTouch(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and player.TeamColor == script.Parent.BrickColor and not debounce then
debounce = true
Instance.new("ForceField", player.Character)
if player.Backpack:FindFirstChild("Gun") then
player.Backpack.Gun:Destroy()
elseif player.Character:FindFirstChild("Gun") then
player.Character.Gun:Destroy()
end
wait(0.1)
debounce = false
end
end
1 comment
elseif shouldn't have a space.