This is the script I used before and I am using now.
debounce = false
function onTouch(part)
local hitplayer = game.Players:GetPlayerFromCharacter(part.Parent)
local hitter = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)--change this to get the player it is being used by.
if hitplayer.TeamColor ~= hitter.TeamColor then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if debounce == false then
if (humanoid ~= nil) then
humanoid.Health = humanoid.Health - 35
debounce = true
end
if debounce == true then
wait(2)
debounce = false
end
end
end
end
script.Parent.Touched:connect(onTouch)
You need to check if the part that touched the block is a part of a character model to begin with. :GetPlayerFromCharacter() returns nil if it can’t find any results. Use that result in an if statement to check if part that is touched is a character model part or not then continue with the rest of the code depending on that result.
debounce = false
function onTouch(part)
local hitplayer = game.Players:GetPlayerFromCharacter(part.Parent)
local hitter = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)--change this to get the player it is being used by.
if hitplayer ~= nil then
if hitplayer.TeamColor ~= hitter.TeamColor then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if debounce == false then
if (humanoid ~= nil) then
humanoid.Health = humanoid.Health - 35
debounce = true
end
if debounce == true then
wait(2)
debounce = false
end
end
end
end
end
script.Parent.Touched:connect(onTouch)
You should use Humanoid:TakeDamage(35) instead. This function allows ForceFields to protect against the damage source
Here’s a compressed version of your script with an easy fix for your error
debounce = false
function onTouch(part)
local hitplayer = game.Players:GetPlayerFromCharacter(part.Parent)
local hitter = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)--change this to get the player it is being used by.
if hitplayer and hitter and hitplayer.TeamColor ~= hitter.TeamColor then
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid and not debounce then
humanoid:TakeDamage(35)
debounce = true
wait(2)
debounce = false
end
end
end
script.Parent.Touched:connect(onTouch)
You just want to make sure both hitplayer and hitter are valid objects before you try to index TeamColor. The error is happening because you are trying to read the TeamColor property of a nil value when one of the player variables is missing.
Actually, there is a easier way to do it; just use a local script, get the local player who is currently holding it (“hitter”) and then fire a remoteEvent to add the damage to the player who touched it.
Please explain how it didn’t work if it really didn’t, such as supplying console logs or pictures/videos of what’s going on. One sentence isn’t sufficient enough to understand what went wrong in your script. Try doing some debugging as well.
Do you have a guaranteed hierarchy? Frankly a wild idea and why you shouldn’t be putting scripts for tools anywhere except directly under the tool instance - having that to reference from would make this much easier to track down, since the equipping character would only be a parent away.
local hitter = game:GetService("Players"):GetPlayerFromCharacter(script:FindFirstAncestorOfClass("Tool").Parent)