To implement the behavior you've described in Roblox, we'll need to set up both server-side and client-side scripts. Here's a step-by-step guide along with the necessary scripts:
1. Setup:
-
Parts Needed:
- A trigger part that the player touches to start the event (let's name it
TriggerPart).
- "Hide" parts that the player can touch to hide from the NPC (named
hide).
- An NPC model named
npc stored in ServerStorage.
- A spawn location for the NPC (we'll use an object named
NPCSpawnLocation in the Workspace).
-
RemoteEvents:
- Create two
RemoteEvent instances in ReplicatedStorage:
StartShakeEvent
StopShakeEvent
2. Server Script:
Place this script inside the TriggerPart. This script handles spawning the NPC, making it chase the player, and monitoring if the player is hiding or gets caught.
-- Server Script inside TriggerPart
local TouchPart = script.Parent
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local NPC = ServerStorage:FindFirstChild("npc")
local StartShakeEvent = ReplicatedStorage:WaitForChild("StartShakeEvent")
local StopShakeEvent = ReplicatedStorage:WaitForChild("StopShakeEvent")
TouchPart.Touched:Connect(function(hit)
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
-- Prevent multiple triggers
TouchPart:Destroy()
-- Summon NPC
local npcClone = NPC:Clone()
npcClone.Parent = workspace
-- Position NPC at spawn location
npcClone:MoveTo(workspace:WaitForChild("NPCSpawnLocation").Position)
-- Notify client to start screen shake
StartShakeEvent:FireClient(player)
-- NPC chase logic
local humanoid = npcClone:WaitForChild("Humanoid")
local humanoidRootPart = npcClone:WaitForChild("HumanoidRootPart")
local function chasePlayer()
if npcClone and npcClone.Parent and character and character.Parent then
humanoid:MoveTo(character.HumanoidRootPart.Position)
end
end
local chaseConnection
chaseConnection = RunService.Stepped:Connect(function()
chasePlayer()
end)
-- Detect when NPC touches the player
local touchedConnection
touchedConnection = npcClone.HumanoidRootPart.Touched:Connect(function(otherPart)
if otherPart:IsDescendantOf(character) then
print("GAME OVER")
-- Clean up
if npcClone then npcClone:Destroy() end
StopShakeEvent:FireClient(player)
chaseConnection:Disconnect()
touchedConnection:Disconnect()
end
end)
-- Monitor if player is hiding
local hidingCheckConnection
hidingCheckConnection = RunService.Heartbeat:Connect(function()
if character and character.Parent then
local isHiding = false
for _, part in ipairs(character:GetDescendants()) do
if part:IsA("BasePart") then
local touchingParts = part:GetTouchingParts()
for _, touchedPart in ipairs(touchingParts) do
if touchedPart.Name == "hide" then
isHiding = true
break
end
end
end
if isHiding then break end
end
if isHiding then
-- Player is hiding
if npcClone then npcClone:Destroy() end
StopShakeEvent:FireClient(player)
-- Clean up
chaseConnection:Disconnect()
touchedConnection:Disconnect()
hidingCheckConnection:Disconnect()
end
else
-- Player character removed
hidingCheckConnection:Disconnect()
end
end)
end
end)
3. LocalScript for Screen Shake:
Place this LocalScript in StarterPlayerScripts so that it's automatically placed in each player's PlayerScripts. This script handles the screen shake effect.
-- LocalScript in StarterPlayerScripts
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local StartShakeEvent = ReplicatedStorage:WaitForChild("StartShakeEvent")
local StopShakeEvent = ReplicatedStorage:WaitForChild("StopShakeEvent")
local shaking = false
local originalCFrame = nil
StartShakeEvent.OnClientEvent:Connect(function()
shaking = true
originalCFrame = camera.CFrame
end)
StopShakeEvent.OnClientEvent:Connect(function()
shaking = false
-- Reset camera to original position
if originalCFrame then
camera.CFrame = originalCFrame
end
end)
RunService.RenderStepped:Connect(function()
if shaking then
-- Apply screen shake effect
local shakeMagnitude = 0.5 -- Adjust intensity here
local shakeCFrame = CFrame.new(
math.random(-shakeMagnitude, shakeMagnitude) / 100,
math.random(-shakeMagnitude, shakeMagnitude) / 100,
math.random(-shakeMagnitude, shakeMagnitude) / 100
)
camera.CFrame = camera.CFrame * shakeCFrame
end
end)
4. NPC Model:
Ensure your NPC model named npc in ServerStorage has a Humanoid and HumanoidRootPart. This script assumes the NPC can be moved using Humanoid:MoveTo().
5. Notes on the "hide" Parts:
- Make sure all your hiding spots are parts named
hide.
- These parts should be sizable so that the player can touch them.
- They can be invisible if you set their
Transparency to 1 and CanCollide to false if you don't want them to obstruct the player.
6. Adjustments and Optimizations:
- Performance: The method of checking
GetTouchingParts() every frame can be resource-intensive if overused. For larger games or more complex scenarios, consider using Region3, raycasting, or custom collision groups.
- Screen Shake Effect: The shaking effect is applied by slightly adjusting the camera's
CFrame every frame. Depending on your game's needs, you might want to implement a more sophisticated shaking effect using tweens or perlin noise for smoother transitions.
- Game Over Logic: Currently, the script simply prints "GAME OVER" to the output. You should replace this with your game's logic for handling the player's defeat, such as showing a "Game Over" screen or restarting the level.
By implementing these scripts and following the setup, your game should now have the desired functionality: when a player touches a part, an NPC is summoned and chases them while their screen shakes. If the NPC touches the player, "GAME OVER" is printed. However, if the player is touching a part named hide during this event, the NPC is deleted, and the screen shake stops.