Roblox how to make it so that you can't deal damage to yourself?

The gun I made uses the Roblox slingshot scripts.

Pelletscript (Altered for my game)



local debris = game:service("Debris")
pellet = script.Parent
local player = pellet:FindFirstChild("creator").Value
damage = 200


function onTouched(hit)
	if not hit or not hit.Parent then return end
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		tagHumanoid(humanoid)
		if humanoid.Health > 0 then  
			humanoid:TakeDamage(damage)
			if humanoid.Health <= 0 then
				local kills = player.leaderstats.Kills
				kills.Value = kills.Value + 1
			end
		end
	else
		damage = damage / 2
		if damage < 1 then
			connection:Disconnect()
			pellet.Parent = nil
		end
	end
end

function tagHumanoid(humanoid)
	-- todo: make tag expire
	local tag = pellet:FindFirstChild("creator")
	if tag then
		-- kill all other tags
		while(humanoid:FindFirstChild("creator")) do
			humanoid:findFirstChild("creator").Parent = nil
		end

		local new_tag = tag:Clone()
		new_tag.Parent = humanoid
		debris:AddItem(new_tag, 1)
	end
end

connection = pellet.Touched:Connect(onTouched)

r = game:service("RunService")
t, s = r.Stepped:Wait()
d = t + 2.0 - s
while t < d do
	t = r.Stepped:Wait()
end

pellet:Destroy()

If you’ve ever used the slingshot model in studio, you might realize that if the slingshot pellet hits you, you deal damage. How do I make it so that you can’t deal damage to yourself?

You would need some way of detecting what player was the one who sent the shot. Often this is done inside of the tool and will depend on the method you are using (for example how a sword normally works is, there is a touched event in the script but if the thing the sword touched is the humanoid who has the sword then it does not do damage; the check if via an if statement).

I don’t really understand why you sent the code on this post because unless I missed it when reviewing I don’t see anywhere it causes damage only detected of when the humanoid died.

Hi. Thank you for answering. I tried doing that but everything I tried wouldn’t work which is why I came to the Devforum. I tried using the creator tag but I just couldn’t figure it out.

The problem is easily fixable with the @LifeDigger’s solution, but why you sent the leaderboard script though? You should have sent the slingshot weapon script.

1 Like

I’ll send the slingshot script, sorry.

I just dont know how to execute LifeDiggers solution.

Send the slingshot script so we can figure it out.

Well there is not much we can do to help if like what @AlternativeOrNot has said, if you don’t send the code that causes the damage to the humanoid.

The code you have sent is for the leaderboard as also stated by @AlternativeOrNot and not the code which damages the humanoid.

1 Like
Tool = script.Parent

local MouseLoc = Tool:WaitForChild("MouseLoc")

VELOCITY = 85 -- constant

local Pellet = Instance.new("Part")
Pellet.Locked = true
Pellet.BackSurface = 0
Pellet.BottomSurface = 0
Pellet.FrontSurface = 0
Pellet.LeftSurface = 0
Pellet.RightSurface = 0
Pellet.TopSurface = 0
Pellet.Shape = 0
Pellet.Size = Vector3.new(2,2,2)
Pellet.BrickColor = BrickColor.new(0,0,0)
script.Parent.PelletScript:Clone().Parent = Pellet

local Mesh = Instance.new("SpecialMesh")
Mesh.Parent = Pellet
Mesh.MeshId = "http://www.roblox.com/asset/?id=151980473"

function fire(mouse_pos)


	Tool.Handle.SlingshotSound:Play()
	local R = Random.new():NextNumber(0.8, 2)
	Tool.Handle.SlingshotSound.Pitch = math.round(R*10)/10
	print(R)

	-- find player's head pos

	local vCharacter = Tool.Parent
	local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)

	local head = vCharacter:FindFirstChild("Head")
	if not head then return end

	local dir = mouse_pos - head.Position
	dir = computeDirection(dir)

	local launch = head.Position + 5 * dir

	local delta = mouse_pos - launch

	local dy = delta.y

	local new_delta = Vector3.new(delta.x, 0, delta.z)
	delta = new_delta

	local dx = delta.magnitude
	local unit_delta = delta.unit

	-- acceleration due to gravity in RBX units
	local g = (-9.81 * 20)

	local theta = computeLaunchAngle( dx, dy, g)

	local vy = math.sin(theta)
	local xz = math.cos(theta)
	local vx = unit_delta.x * xz
	local vz = unit_delta.z * xz


	local missile = Pellet:Clone()




	missile.Position = launch
	missile.Velocity = Vector3.new(vx,vy,vz) * VELOCITY

	missile.PelletScript.Disabled = false

	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = vPlayer
	creator_tag.Name = "creator"
	creator_tag.Parent = missile

	missile.Parent = workspace

end



function computeLaunchAngle(dx,dy,grav)
	-- arcane
	-- http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

	local g = math.abs(grav)
	local inRoot = (VELOCITY*VELOCITY*VELOCITY*VELOCITY) - (g * ((g*dx*dx) + (1*dy*VELOCITY*VELOCITY)))
	if inRoot <= 0 then
		return .25 * math.pi
	end
	local root = math.sqrt(inRoot)
	local inATan1 = ((VELOCITY*VELOCITY) + root) / (g*dx)

	local inATan2 = ((VELOCITY*VELOCITY) - root) / (g*dx)
	local answer1 = math.atan(inATan1)
	local answer2 = math.atan(inATan2)
	if answer1 < answer2 then return answer1 end
	return answer2
end

function computeDirection(vec)
	local lenSquared = vec.magnitude * vec.magnitude
	local invSqrt = 1 / math.sqrt(lenSquared)
	return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end




Tool.Enabled = true
function onActivated()

	if not Tool.Enabled then
		return
	end

	Tool.Enabled = false

	local character = Tool.Parent;
	local humanoid = character.Humanoid
	if not humanoid  then
		print("Humanoid not found")
		return 
	end

	local targetPos = MouseLoc:InvokeClient(game:GetService("Players"):GetPlayerFromCharacter(character))

	fire(targetPos)

	wait(1)

	Tool.Enabled = true
end

script.Parent.Activated:Connect(onActivated)

@LifeDigger @AlternativeOrNot

Oh sorry Im so dumb. I thought I sent the script that did damage already but I accidentally sent my leaderstats script. I replaced the script above.

Slingshot script doesn’t seem to be having humanoid damaging system anywhere, can you send the Pellet’s script too?

1 Like


local debris = game:service("Debris")
pellet = script.Parent
local player = pellet:FindFirstChild("creator").Value
damage = 200


function onTouched(hit)
	if not hit or not hit.Parent then return end
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		tagHumanoid(humanoid)
		if humanoid.Health > 0 then  
			humanoid:TakeDamage(damage)
			if humanoid.Health <= 0 then
				local kills = player.leaderstats.Kills
				kills.Value = kills.Value + 1
			end
		end
	else
		damage = damage / 2
		if damage < 1 then
			connection:Disconnect()
			pellet.Parent = nil
		end
	end
end

function tagHumanoid(humanoid)
	-- todo: make tag expire
	local tag = pellet:FindFirstChild("creator")
	if tag then
		-- kill all other tags
		while(humanoid:FindFirstChild("creator")) do
			humanoid:findFirstChild("creator").Parent = nil
		end

		local new_tag = tag:Clone()
		new_tag.Parent = humanoid
		debris:AddItem(new_tag, 1)
	end
end

connection = pellet.Touched:Connect(onTouched)

r = game:service("RunService")
t, s = r.Stepped:Wait()
d = t + 2.0 - s
while t < d do
	t = r.Stepped:Wait()
end

pellet:Destroy()

In onTouched function, before doing the code in if humanoid then, you should check if humanoid is your humanoid. If it is, then we return, otherwise do the main part of code.

Since the creator value is your player by default, we can easily check if hit.Parent is our player.

if humanoid then
	if humanoid.Parent ~= player.Character then
		tagHumanoid(humanoid)
		if humanoid.Health > 0 then  
			humanoid:TakeDamage(damage)
			if humanoid.Health <= 0 then
				local kills = player.leaderstats.Kills
				kills.Value = kills.Value + 1
			end
		end
	else return end
else
	damage = damage / 2
		if damage < 1 then
			connection:Disconnect()
			pellet.Parent = nil
		end
	end

little explanation:
we check if humanoid.Parent is player.Character, if it is, we return, otherwise we tag humanoid, damage it and etc.

1 Like

is this correct? I get red and blue lines under lines 14, 26, 55

local debris = game:service("Debris")
pellet = script.Parent
local player = pellet:FindFirstChild("creator").Value
damage = 200


function onTouched(hit)
	if not hit or not hit.Parent then return end
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
	if humanoid.Parent ~= player.Character then
		tagHumanoid(humanoid)
		if humanoid.Health > 0 then  
			humanoid:TakeDamage(damage)
			if humanoid.Health <= 0 then
				local kills = player.leaderstats.Kills
				kills.Value = kills.Value + 1
			end
		end
	else return end
else
	damage = damage / 2
		if damage < 1 then
			connection:Disconnect()
			pellet.Parent = nil
		end
	end

function tagHumanoid(humanoid)
	-- todo: make tag expire
	local tag = pellet:FindFirstChild("creator")
	if tag then
		-- kill all other tags
		while(humanoid:FindFirstChild("creator")) do
			humanoid:findFirstChild("creator").Parent = nil
		end

		local new_tag = tag:Clone()
		new_tag.Parent = humanoid
		debris:AddItem(new_tag, 1)
	end
end

connection = pellet.Touched:Connect(onTouched)

r = game:service("RunService")
t, s = r.Stepped:Wait()
d = t + 2.0 - s
while t < d do
	t = r.Stepped:Wait()
end

pellet:Destroy()
1 Like

What do you mean by “red and blue lines”? Errors?

1 Like

bdf
csa

1 Like

I fixed the code, here it is:

local debris = game:service("Debris")
pellet = script.Parent
local player = pellet:FindFirstChild("creator").Value
damage = 200


function onTouched(hit)
	if not hit or not hit.Parent then return end
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		if humanoid.Parent ~= player.Character then
			tagHumanoid(humanoid)
			if humanoid.Health > 0 then  
				humanoid:TakeDamage(damage)
				if humanoid.Health <= 0 then
					local kills = player.leaderstats.Kills
					kills.Value = kills.Value + 1
				end
			end
		else return end
	else
		damage = damage / 2
		if damage < 1 then
			connection:Disconnect()
			pellet.Parent = nil
		end
	end
end

function tagHumanoid(humanoid)
	-- todo: make tag expire
	local tag = pellet:FindFirstChild("creator")
	if tag then
		-- kill all other tags
		while(humanoid:FindFirstChild("creator")) do
			humanoid:findFirstChild("creator").Parent = nil
		end

		local new_tag = tag:Clone()
		new_tag.Parent = humanoid
		debris:AddItem(new_tag, 1)
	end
end

connection = pellet.Touched:Connect(onTouched)

r = game:service("RunService")
t, s = r.Stepped:Wait()
d = t + 2.0 - s
while t < d do
	t = r.Stepped:Wait()
end

pellet:Destroy()

About the “blue lines”: these are pretty common and don’t affect anything, it just tells you to make variables local - of course it is not neccessary. But I checked the fixed version - there shouldn’t be “red lines” nor “blue ones”.

2 Likes

Thank you, I learned a lot. I will keep this knowledge for my upcoming game! your a lifesaver :smiley:

1 Like