[HELP] Attempting to make a Scare Actor play an animation

Information

I am not a very great programmer at all, but I’m looking to make a NPC play a animation when I walk by it and audio to play also when walking by it. I am trying to make like a scare actor type thing where they scare the player and play the scare auto.

Questions

  1. What do you want to achieve? I have spoken above what I am trying to achieve.

  2. What is the issue? I cannot get the animation to play when walking by it or have it play audio when I walk by it.

  3. What solutions have you tried so far? Unfortunately, I have not.

Script

Here is the script I’ve tried to use as much as I could.

-- So here I tried to script the invisible part you touch to play the animation.
local function onTouch(hit)
	wait(0.1)
	play:ScareAnimation1_flameon875
	wait(5)
end

script.Parent.Touched:Connect(onTouch)

-- It did not work, but I'm not sure if it was the wrong Parent or where I put it
-- because I put the part inside the character and then put the Animation inside
-- the part.

Reply Solution

Please comment below any possible ways to do it, apologies if it is in the wrong category.

Signature

Signed,
flameon875

2 Likes

Hi. You’re wanting the player to be in at least a certain distance, correct? If so, you would need to use magnitude. That can help you find distance.

local MAX_DISTANCE = 10 -- in studs
local Player = game.Players.LocalPlayer
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart
local npcRootPart = pathtoNpc.Character.HumanoidRootPart

game:GetService("RunService").Hearbeat:Connect(function()
    if (HumanoidRootPart.Position - npcRootPart.Position).magnitude <= MAX_DISTANCE then
        --do stuff
    end
end

Now, the code I wrote was mostly quick and I want to explain something. You need to put this in a local script, and I recommend having a function and calling that in Heartbeat. Running this on the server will cause a lot of stress. Hopefully this helps!

1 Like

Thank you for the reply, I’ll test it out and get back to you if it works. :slight_smile:

Unfortunately, the script did not work. I put it in a local script but I didn’t understand the Heartbeat part. Maybe if you give that an explanation it could work. Other than that, it did not work. :frowning:

The script does not work because you need to fill in the area with the comment “do stuff”

Place your code there, and it will run whenever a player gets near enough to the part.

There are some parts that you will have to fill in/change in order for the script to work. Such as, the

Instead of “pathToNpc…” , the line should reference your “scare actor” Model.

eg.

local npcRootPart = game.Workspace[" type the npc name here"].HumanoidRootPart

To add on to his response, in order to play an animation on an NPC, you will need to use a Server-Sided script. You’ll also need to use a remote event to fire the animation to the server whenever the player is close enough. You can learn more about playing an animation here, and you can learn about Remote Events here. To keep things simple, I’m going to provide you with the complete script.

Client

local MAX_DISTANCE = 10 -- in studs
local Player = game.Players.LocalPlayer
local Character = Player.Character
local HumanoidRootPart = Character.HumanoidRootPart
local npcRootPart = pathtoNpc.Character.HumanoidRootPart

local PrimaryEvent = game.ReplicatedStorage.pathtoRemoteEvent --I placed the even in the Replicated Storage since the client and the server can access it

game:GetService("RunService").Hearbeat:Connect(function()
    if (HumanoidRootPart.Position - npcRootPart.Position).magnitude <= MAX_DISTANCE then
           PrimaryEvent:FireServer("Scare")
    end
end

Server

local PrimaryEvent = Instance.new("Remote Event", game.ReplicatedStorage)
PrimaryEvent.Name = "PrimaryEvent"

local ScareAnimID = Instance.new("Animation")
ScareAnimID.AnimationId = "rbxassetid://INSERTID"

local NPC = pathtoNPC

local ScareAnimation = NPC.Humanoid:LoadAnimation(ScareAnimID)

function onReceived(player, info)
     if info == "Scare" then
          ScareAnimation:Play()
    end
end
PrimaryEvent.OnServerEvent:Connect(onReceived)

Love the code, I’ll make sure to check out the script and tell you if it has worked or not. :slight_smile:

I came to ask where would you put the Cilent and Server scripts.

Also, the script did not work correctly as intended.

I think you got a bit confused.

The script will not work if you do not change the variables that are provided in scripts that @McRocketNuggets and @quamatic made.

I understand that you may not be very well versed in scripting, but please understand that we are here to assist you in your scripting, and not to provide you with scripts that you request.

1 Like