You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? I want to make this system work for my gas station robbery.
-
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.
-
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
}