Gas Robbery System

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to make this system work for my gas station robbery.

  2. What is the issue? Well, I’m developing a prison game along some friends and I got the task of doing the Gas Station robbery, but we wanna make this a little bit different and make it that the NPC raise his hands when you aim to it, the system worked fine thanks to @Mystxry12 < – He’s amazing not gonna lie. Okay back to the issue, when I “tried” to make it work in my gas station system it just doesn’t work. Because I have to use a tool, but in a prison game you know that you gonna have a lot of guns.

  3. What solutions have you tried so far? I tried a for loop through all the player backpack and search for the tools, but that doesn’t work.

Here’s the code that works.

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local tool = script.Parent or error("Script must be a child of a tool")
local npc = workspace.Noob

local isEquipped = false

tool.Equipped:Connect(function()
	isEquipped = true
end)

tool.Unequipped:Connect(function()
	isEquipped = false
end)

-- Thanks to @nicemike40 for this function
local function IsPlayerInLineOfSight(playerHead, npcHead, maxAngle)
	local lookDirection = playerHead.CFrame.LookVector
	local towardsNpc = (npcHead.Position - playerHead.Position).Unit
	local dotProduct = lookDirection:Dot(towardsNpc)
	local cosineAngle = math.cos(math.rad(maxAngle))
	return dotProduct >= cosineAngle
end

local function PlayLoadedAnimation()
	print("Is working ig")
end

RunService.Stepped:Connect(function()
	if isEquipped and npc and npc:FindFirstChild("Head") and character and character:FindFirstChild("Head") then
		local playerHead = character.Head
		local npcHead = npc.Head

		if IsPlayerInLineOfSight(playerHead, npcHead, 60) then
			PlayLoadedAnimation()
		end
	end
end)

Here’s the one that doesn’t

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local npc = workspace.Noob
local isEquipped = {}

local function IsPlayerInLineOfSight(playerHead, npcHead, maxAngle)
	local lookDirection = playerHead.CFrame.LookVector
	local towardsNpc = (npcHead.Position - playerHead.Position).Unit
	local dotProduct = lookDirection:Dot(towardsNpc)
	local cosineAngle = math.cos(math.rad(maxAngle))
	return dotProduct >= cosineAngle
end

local function PlayLoadedAnimation()
	print("Loaded animation played!")
	-- Implement your code to play the loaded animation here
end

local function CheckEquipped()
	local player = Players.LocalPlayer
	local backpack = player and player.Backpack
	if backpack then
		for _, tool in ipairs(backpack:GetChildren()) do
			if tool:IsA("Tool") then
				isEquipped[tool] = true
			end
		end
	end
end

local function CheckLineOfSight()
	local player = Players.LocalPlayer
	local character = player and player.Character
	if npc and npc:FindFirstChild("Head") and character and character:FindFirstChild("Head") then
		local playerHead = character.Head
		local npcHead = npc.Head

		if isEquipped[next(isEquipped)] and IsPlayerInLineOfSight(playerHead, npcHead, 60) then
			PlayLoadedAnimation()
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		wait()
		CheckEquipped()
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	isEquipped = {}
end)

game:GetService("Backpack").ChildAdded:Connect(function(child)
	if child:IsA("Tool") then
		isEquipped[child] = true
	end
end)

game:GetService("Backpack").ChildRemoved:Connect(function(child)
	if child:IsA("Tool") then
		isEquipped[child] = nil
	end
end)

RunService.Stepped:Connect(CheckLineOfSight)

return {
	CheckEquipped = CheckEquipped
}

2 Likes

What specificaly isnt working, and can you add some print statements and tell me what doesnt work?

1 Like

Welll when I aim to the player

local function PlayLoadedAnimation()
	print("Loaded animation played!")
	-- Implement your code to play the loaded animation here
end

This function should be fired, but I am aiming to they player and nothing is happening, that means that the function is not working when I aim to the player

It also gave to me this error ReplicatedStorage.Main.Game.Robbery.SmallRobbery.NpcController:56: attempt to index nil with ‘ChildAdded’ in the line 56

Backpack isn’t a service, did you mean to use player.Backpack?

That makes, sense. Let me try to change it

It’s working now, had to ask chat GPT, but now is firing the function even before I aim the gun to the npc :frowning: Here’s the code:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local isEquipped = {}

local function IsPlayerInLineOfSight(playerHead, npcHead, maxAngle)
	local lookDirection = playerHead.CFrame.LookVector
	local towardsNpc = (npcHead.Position - playerHead.Position).Unit
	local dotProduct = lookDirection:Dot(towardsNpc)
	local cosineAngle = math.cos(math.rad(maxAngle))
	return dotProduct >= cosineAngle
end

local function PlayLoadedAnimation()
	print("Raised hand!")
end

local function CheckLineOfSight()
	local npc = workspace.Noob
	if npc and npc:FindFirstChild("Head") and character and character:FindFirstChild("Head") then
		local playerHead = character.Head
		local npcHead = npc.Head

		if IsPlayerInLineOfSight(playerHead, npcHead, 60) then
			PlayLoadedAnimation()
		end
	end
end

local function CheckEquipped()
	local backpack = player:FindFirstChild("Backpack")
	if backpack then
		for _, tool in ipairs(backpack:GetChildren()) do
			if tool:IsA("Tool") then
				isEquipped[tool] = true
			end
		end
	end

	CheckLineOfSight()
end

local function OnPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		wait() -- Wait for the character to fully load
		CheckEquipped()
	end)
end

local function OnPlayerRemoving(player)
	isEquipped = {}
end

local function OnChildAdded(child)
	if child:IsA("Tool") then
		isEquipped[child] = true
		CheckLineOfSight()
	end
end

local function OnChildRemoved(child)
	if child:IsA("Tool") then
		isEquipped[child] = nil
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
player.Backpack.ChildAdded:Connect(OnChildAdded)
player.Backpack.ChildRemoved:Connect(OnChildRemoved)
RunService.Stepped:Connect(CheckLineOfSight)

return {
	CheckEquipped = CheckEquipped
}

I managed to make it work ! Thanks homie

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.