Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to check if the name of a model does not match the name of a player firing a bullet at it?

I am trying to enable PvP in an FPS game I am working on, and I'm using a combination of 3 scripts to make it work, but it doesn't. I am using a local script to fire an event for a character script to pick up, which clones a bullet from ReplicatedStorage running on velocity. But when I try to shoot something, it doesn't take damage. Here are the scripts:

Local Script:

repeat wait() until game.Players.LocalPlayer.Character

local shootdebounce = false
local player = game.Players.LocalPlayer
local character = player.Character
local cam = workspace.CurrentCamera
local mouse = player:GetMouse()
local rep = game:GetService("ReplicatedStorage")
local gun = rep.Pistol:Clone()
gun.Parent = game.Workspace
local run = game:GetService("RunService")
local uis = game:GetService("UserInputService")

mouse.Icon = 'rbxassetid://316279304'
uis.MouseIconEnabled = true

local walking = false

local anim = gun:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Animation)
local sAnim = gun:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Shoot)

local cf = CFrame.new()

local aimming = false
local shooting = false

mouse.Button2Down:Connect(function()
	aimming = true
	uis.MouseIconEnabled = false
end)

mouse.Button2Up:Connect(function()
	aimming = false
	uis.MouseIconEnabled = true
end)

run.RenderStepped:connect(function()
	if aimming == false then
		cf = cf:lerp(CFrame.new(0.5,-1.5,0.5)*CFrame.Angles(0,0,0),0.2)
		gun:SetPrimaryPartCFrame(cam.CFrame*cf)
		local Properties = {FieldOfView = 70} -- change the number as you wish
		local Info = TweenInfo.new(0.05, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
		local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)
		T:Play()
	end
end)

run.RenderStepped:connect(function()
	if aimming == true then
		cf = cf:lerp(CFrame.new(-0.75,-0.75,0.5)*CFrame.Angles(-0.05,0,0),0.2)
		gun:SetPrimaryPartCFrame(cam.CFrame*cf)
		local Properties = {FieldOfView = 35} -- change the number as you wish
		local Info = TweenInfo.new(0.05, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
		local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties)
		T:Play()
		uis.MouseIconEnabled = false
	end
end)

character.Humanoid.Running:connect(function(speed)
	print(speed)
	if speed > 8  and speed < 24 then
		walking = true
		anim:Play()
	else
		anim:Stop()
		walking = false
	end
end)

run.RenderStepped:Connect(function()
end)

mouse.Button1Down:Connect(function()
	sAnim:Play()
	if aimming == false then
		character.HipFire:FireServer(CFrame.new(character:WaitForChild("FirePart").Position,mouse.Hit.p).lookVector)
	else
		character.AimFire:FireServer(CFrame.new(character:WaitForChild("Head").Position,mouse.Hit.p).lookVector)
	end
end)

Server Character Script:

local bullet = game.ReplicatedStorage.Bullet
local bulletSpeed = 100

script.Parent:WaitForChild("FirePart").Decal.Name = script.Parent.Name

local function HipFire(player, fireDirection)
	print(player.Name,"Hip-fired his gun")
	local newBullet = bullet:Clone()
	newBullet.Parent = script.Parent
	newBullet.Name = player.Name
	newBullet.Position = script.Parent:WaitForChild("FirePart").Position
	newBullet.Velocity = fireDirection * bulletSpeed
	newBullet.RotVelocity = fireDirection * bulletSpeed
	script.Parent:WaitForChild("Head").Shoot.PlaybackSpeed = math.random() + math.random()
	script.Parent:WaitForChild("Head").Shoot:Play()
	wait(10)
	newBullet:Destroy()
	local newBullet = nil
end

local function AimFire(player, fireDirection)
	print(player.Name,"Aimed and fired his gun")
	local newBullet = bullet:Clone()
	newBullet.Parent = script.Parent
	newBullet.Name = player.Name
	newBullet.Position = script.Parent:WaitForChild("Head").Position
	newBullet.Velocity = fireDirection * bulletSpeed
	newBullet.RotVelocity = fireDirection * bulletSpeed
	script.Parent:WaitForChild("Head").Shoot.PlaybackSpeed = math.random() + math.random()
	script.Parent:WaitForChild("Head").Shoot:Play()
	wait(10)
	newBullet:Destroy()
	local newBullet = nil
end

script.Parent.HipFire.OnServerEvent:Connect(HipFire)
script.Parent.AimFire.OnServerEvent:Connect(AimFire)

Bullet Server Script:

local Debounce = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and Debounce == false then
		if not hit.Parent.Name == script.Parent.Name then
			Debounce = true
			hit.Parent.Humanoid:TakeDamage(16)
			if hit.Name == "Head" then
				hit.Parent.Torso.Explode.PlaybackSpeed = math.random() + math.random() 
				hit.Parent.Torso.Explode:Play()
				hit.Parent.Torso.Blood.ParticleEmitter.Enabled = true
				hit.Name = "nil"
				hit.Parent.Part.Name = "Head"
				hit.BrickColor = BrickColor.new("Really red")
				hit.Parent.Torso.Neck:Destroy()
				hit.Parent.Humanoid.Health = 0
			end
			hit.Velocity = script.Parent.Velocity / 2
			hit.RotVelocity = script.Parent.Velocity / 2
			script.Parent.Parent.Humanoid:TakeDamage(-10)
			script.Parent.CanCollide = true
			script.Parent.Transparency = 0
			script.Parent.Velocity = script.Parent.Velocity * 3/5
			Debounce = false
		end
	end
end)

1 answer

Nevermind, I solved it. It was a matter of replacing the "if not" with a ~= operator.

This discussion is preserved as it was. New answers, comments, and votes are closed.