Help me with this OnTouched Function

I am making a horror game and I want to make a jumpscare when a part is touched so I made a Touched function that moves a different part to the player and it makes a sound.

But, it doesn’t work for some reason. i have no idea why, there are no error messages or anything in the script.

Ive looked on youtube and have asked people but unfortunately, I haven’t gotten anything back from them.

Screenshot of the script:

Can you provide more here? What is “game.Workspace.Jumpscare” and why are you playing it?

Also, you wrote:
game.Workspace.Monster = Vector3.new(100, 100, 100)

This wouldn’t work because you have to write:
game.Workspace.Monster.Position = Vector3.new(100, 100, 100)
Remember to write the property that you’re changing not just the instance!

Also, if you want something to move on the player’s character, you can get the “hit” of the touched event. For example, you can rewrite your code like this:

local part = script.Parent

part.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChild(“Humanoid”) then
      game.Workspace.Monster.Position = hit.Parent.HumanoidRootPart.Position
      game.Workspace.Jumpscare:Play()
   end
end)

Always check for a humanoid in touched events because anything can touch a part, including another part, so you need to make sure that a player is touching it by using an if statement.

3 Likes

game.Worskpace.Jumpscare is a sound and I am playing it for the jumpscare

Ok, that makes sense. Make sure to look into what I said.

Ok, I will try it out and see what happens

So it didnt work there was a new error message when I touched the part

"Position is not a a valid member of Model “Workspace.Monster”

Ah, I didn’t know that Monster is a model; I thought it was a part. “Position” is a property that parts have. Models don’t have this property which explains why “Position” isn’t a member of Monster. Can you show me the children of the monster model?

Piggybacking off of dodle’s answer, I would recommend you to set a primary part for the monster, and then use :SetPrimaryPartCFrame() to adjust the whole monster’s position.

1 Like

Yeah:

Its basically just a bunch of parts and a mesh into one model

Yeah, I agree with @NINJAMASTR999. Make the Primary Part a single part of the model.

By doing this, all parts of the model will move in respect to a part of the model that you choose.

Alright, Trying it right now, hopefully it works.

1 Like

Where would I put the :SetPrimaryPartCFrame() ?

Replace game.Workspace.Monster.Position = Vector3.new() with game.Workspace.Monster:SetPrimaryPartCFrame(-InsertCFrameHere-).

Try this.

local Part = script.Parent

local function jumpscare()
     game.StarterGui.YourJumpScareGui.Enabled = true -- UI looks better in jumpscares imo, if you want to make it a mesh/model, teleport it in front of the player.
     game.Workspace.Jumpscare.Playing = true -- Make sure the volume is up
end

Part.Touched:Connect(jumpscare)

If this doesn’t work, let me know. Also, are there any errors in the output?