So I'm making an AI with a magnitude script that plays music when he chases you. The problem is, when you respawn for some reason, the music starts as if you entered the vicinity of the enemy and it does that every time except for the first time. Can someone please help me fix this? Thanks!
Here are the two scripts that people helped me make:
local part1 = game.Workspace.Neighbor.Torso --The torso of your AI local player = game.Players.LocalPlayer --Gets the player local part2 = game.Workspace:WaitForChild(player.Name):WaitForChild("Torso") --The player local Sound = game.Lighting.Sound --Your Sound, place in PlayerGui for local sound instead of global while true do local magnitude = (part1.Position - part2.Position).magnitude if magnitude < 50 then if not Sound.IsPlaying then Sound:Play() end elseif magnitude >= 50 or game.Workspace:FindFirstChild(player.name):FindFirstChild("Torso") then if Sound.IsPlaying then Sound:Stop() end end wait(0.01) end
Character = script.Parent Humanoid = Character.Humanoid function Check() while true do wait(0.1) if game.Lighting.Sound.IsPlaying then game.Lighting.Sound:Stop() end end end Humanoid.Died:connect(Check) game.Players.LocalPlayer.CharacterAdded:connect(Check)
For the first script is should be:
local part1 = game.Workspace.Neighbor.Torso --The torso of your AI local player = game.Players.LocalPlayer --Gets the player local part2 = game.Workspace:WaitForChild(player.Name):WaitForChild("Torso") --The player local Sound = game.Lighting.Sound --Your Sound, place in PlayerGui for local sound instead of global while true do local magnitude = (part1.Position - part2.Position).magnitude if magnitude < 50 then if not Sound.IsPlaying then Sound:Play() end elseif magnitude >= 50 or game.Workspace:FindFirstChild(player.name):FindFirstChild("Torso") then if Sound.IsPlaying = true then --Add true Sound:Stop() end end wait(0.01) end
For the second script it should be:
Character = script.Parent Humanoid = Character.Humanoid function Check() while true do wait(0.1) if game.Lighting.Sound.IsPlaying = true then --Add true game.Lighting.Sound:Stop() end end end Humanoid.Died:connect(Check) game.Players.LocalPlayer.CharacterAdded:connect(Check)