Certainly! Below is the modified script that performs the camera shake effect for 2 seconds when the player interacts with a ProximityPrompt tagged as "Sinal".
-- Get the necessary services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local CollectionService = game:GetService("CollectionService")
-- Get the local player
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
-- Function to perform camera shake
local function cameraShake(duration)
local cam = workspace.CurrentCamera
local intensity = 25
local range = 55
local startTime = tick()
-- Run the shake effect for the specified duration
while tick() - startTime < duration do
local dist = player:DistanceFromCharacter(character.HumanoidRootPart.Position)
local clamp = math.clamp(1 + (intensity - 0.1)*(1-(dist/range)), 0.1, intensity)
cam.CFrame *= CFrame.Angles(
math.random(-clamp, clamp) / 500,
math.random(-clamp, clamp) / 500,
math.random(-clamp, clamp) / 500
)
TweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {
CFrame = cam.CFrame * CFrame.Angles(0, 0, math.rad(math.random(-clamp/2, clamp/2)))
}):Play()
humanoid.CameraOffset = Vector3.new(
math.random(-clamp, clamp) / 20,
math.random(-clamp, clamp) / 20,
math.random(-clamp, clamp) / 15
) / 55
task.wait()
end
-- Reset the camera offset after shaking
humanoid.CameraOffset = Vector3.new(0, 0, 0)
end
-- Set up the ProximityPrompt interaction
local function setupProximityPrompt()
-- Find all objects tagged with "Sinal"
local sinais = CollectionService:GetTagged("Sinal")
for _, sinal in pairs(sinais) do
local prompt = sinal:FindFirstChildOfClass("ProximityPrompt")
if prompt then
prompt.Triggered:Connect(function()
-- Start the camera shake for 2 seconds when the prompt is triggered
cameraShake(2)
end)
end
end
-- Listen for new objects tagged with "Sinal"
CollectionService:GetInstanceAddedSignal("Sinal"):Connect(function(sinal)
local prompt = sinal:WaitForChild("ProximityPrompt", 5)
if prompt then
prompt.Triggered:Connect(function()
cameraShake(2)
end)
end
end)
end
-- Initialize the script
setupProximityPrompt()
Explanation:
-
Services and Variables:
- We use
CollectionService to find objects tagged with "Sinal".
- We define a
cameraShake function that shakes the camera for a specified duration (2 seconds in this case).
-
Camera Shake Function:
- This function runs a while loop for the duration of the shake effect.
- Inside the loop, it applies random rotations to the camera's
CFrame to create a shake effect.
- It also adjusts the
Humanoid.CameraOffset for additional shaking.
- After the loop ends, it resets the
CameraOffset to its default value.
-
ProximityPrompt Setup:
- We find all instances tagged with
"Sinal" and connect the Triggered event of their ProximityPrompt.
- When the prompt is triggered, the
cameraShake function is called for 2 seconds.
- We also listen for any new instances that get tagged with
"Sinal" during runtime and set up their prompts accordingly.
Notes:
- Ensure that all objects you want to interact with are properly tagged with
"Sinal" using the Tag property or via CollectionService:AddTag(instance, "Sinal").
- Make sure that each tagged object contains a
ProximityPrompt child.
- Place this script in a location where it will run for the local player, such as a
LocalScript inside StarterPlayerScripts or StarterCharacterScripts.
Feel free to adjust the intensity and range variables within the cameraShake function to fine-tune the shake effect to your liking.