Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I script an interaction that leads to a timed event?

Hi! So I'm extremely new to scripting, however I've started creating a few games recently and I want to go beyond the simplicity of putting together some models. The games I'm creating mainly focus on the horror category, and so far I think I've done a decent job for a beginner. Currently I'm in the middle of creating a new game. I've made a map and all but I was wondering how I could create a jumpscare.

To be more specific, say if a player were to click a button, or simply step on an object, I was wondering how I could script a timed event that when completed will make a model pop up in front of them, as well as when this specific object is interacted with, will play a sound. I'm sure it will help give my game a little credit, since for the most part the games I've created so far are just ambiance, nothing too scary. Thanks!

1 answer

✓ Accepted answer

So first, you will want to look into how events work. Every object has a bunch of events that allow you to execute code when something happens. For example, parts have an event called .Touched, and when connected to a function will execute the code in the function when the part is touched.

To move models, you can look into using :SetPrimaryPartCFrame, :MoveTo or TranslateBy, which are all methods for models. They each allow you to move a model relative to whatever its PrimartPart is. You can set the PrimaryPart through the properties tab in studio (by just clicking the part). Then, when you use :MoveTo on a model, you can move the PrimaryPart to the given position, which will bring the rest of the model along with it.

You will also need a debounce to act as a cooldown for the jumpscare.

Lastly, playing sounds is the simplest part. Once you have given the sound a SoundId, you can simply use the method :Play to play the audio.

When combined together, it would look something like this (in a script inside the part that is touched):

local Debounce = true
local JumpscareModel = workspace["Model name here"]

script.Parent.Touched:Connect(function(Hit) --When the part is touched, run the code:
	--Hit will be the part that touched; check if Hit is inside a character:
	local Plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
	if Plr then
		print(Plr.Name .. " has touched the part!")
		
		if Debounce == true then
			Debounce = false

			JumpscareModel:MoveTo(JumpscareModel.PrimaryPart.Position + Vector3.new(0,10,0)) --will move the model 10 studs upwards (on Y axis)

			--if sound is called "JumpscareSound" inside the model:
			JumpscareModel.JumpscareSound:Play()

			wait(3) --move back down after 3 seconds:
			JumpscareModel:MoveTo(JumpscareModel.PrimaryPart.Position + Vector3.new(0,-10,0))
			
			Debounce = true --allow function to be activated again
		end
	end
end)

Hope this helps as an outline!

2 comments

0

Thanks! However when I tried running this script with the instructions given it wouldn't work, and there was an error: 12:57:38.444 - Workspace.PrimaryPart.Script:13: attempt to index field 'PrimaryPart' (a nil value) I can't seem to find a way to fix it. 

0

You have to set the PrimaryPart in the properties of the model.

This discussion is preserved as it was. New answers, comments, and votes are closed.