Script not spawning dummy model from replicated storage

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local Lighting = game:GetService("Lighting")
local StarterGui = game:GetService("StarterGui")

local player = Players.LocalPlayer
local Camera = Workspace.CurrentCamera

local respawnEvent = ReplicatedStorage:WaitForChild("RespawnRequest")

_G.isJumpScareActive = false
_G.isRespawning = false

-- Create or get the Atmosphere object
local atmosphere = Lighting:FindFirstChildOfClass("Atmosphere")
if not atmosphere then
    atmosphere = Instance.new("Atmosphere")
    atmosphere.Parent = Lighting
end

-- Initial Atmosphere settings
local initialDensity = 0.425
local initialColor = Color3.fromRGB(199, 199, 199)
local initialDecay = Color3.fromRGB(106, 112, 125)
local initialGlare = 0
local initialHaze = 0

-- Jump scare Atmosphere settings
local jumpScareDensity = 1
local jumpScareColor = Color3.fromRGB(0, 0, 0)
local jumpScareDecay = Color3.fromRGB(0, 0, 0)
local jumpScareGlare = 0
local jumpScareHaze = 10

-- Function to set Atmosphere settings
local function setAtmosphereSettings(density, color, decay, glare, haze)
    atmosphere.Density = density
    atmosphere.Color = color
    atmosphere.Decay = decay
    atmosphere.Glare = glare
    atmosphere.Haze = haze
end

local function fadeOutSound(soundInstance, startFadeTime, fadeDuration)
    local startTime = tick()
    local initialVolume = soundInstance.Volume

    local function onRenderStep()
        local elapsedTime = tick() - startTime
        if elapsedTime >= startFadeTime and elapsedTime < startFadeTime + fadeDuration then
            local fadeElapsedTime = elapsedTime - startFadeTime
            soundInstance.Volume = initialVolume * (1 - (fadeElapsedTime / fadeDuration))
        elseif elapsedTime >= startFadeTime + fadeDuration then
            soundInstance.Volume = 0
            soundInstance:Destroy()
            RunService:UnbindFromRenderStep("fadeOutSound")
        end
    end

    RunService:BindToRenderStep("fadeOutSound", Enum.RenderPriority.Camera.Value, onRenderStep)
end

local function playRandomJumpScareSound()
    local jumpScareSoundsFolder = ReplicatedStorage:FindFirstChild("JumpScareSounds")
    if not jumpScareSoundsFolder then
        warn("JumpScareSounds folder not found in ReplicatedStorage")
        return
    end

    local sounds = {
        jumpScareSoundsFolder:FindFirstChild("JumpScare1"),
        jumpScareSoundsFolder:FindFirstChild("JumpScare2")
    }

    local soundToPlay = sounds[math.random(#sounds)]

    if soundToPlay then
        local soundInstance = soundToPlay:Clone()
        soundInstance.Volume = 1
        soundInstance.Parent = Camera
        soundInstance:Play()
        soundInstance.Ended:Connect(function()
            soundInstance:Destroy()
        end)
    else
        warn("Selected jump scare sound not found in JumpScareSounds folder")
    end
end

local function playJumpScareStaticSound()
    local jumpScareSoundsFolder = ReplicatedStorage:FindFirstChild("JumpScareSounds")
    if not jumpScareSoundsFolder then
        warn("JumpScareSounds folder not found in ReplicatedStorage")
        return
    end

    local staticSound = jumpScareSoundsFolder:FindFirstChild("Static")
    if not staticSound then
        warn("Static sound not found in JumpScareSounds folder")
        return
    end

    local soundInstance = staticSound:Clone()
    soundInstance.Parent = Camera
    soundInstance:Play()

    -- Fade out the sound starting after 5 seconds, over a duration of 5 seconds
    fadeOutSound(soundInstance, 5, 5)
end

local function setCameraToJumpScareCam(jumpScareCam)
    Camera.CameraType = Enum.CameraType.Scriptable
    Camera.CFrame = jumpScareCam.CFrame * CFrame.new(0, 0, -5)

    local function updateCamera()
        if jumpScareCam.Parent then
            Camera.CFrame = jumpScareCam.CFrame * CFrame.new(0, 0, -5)
        else
            Camera.CameraType = Enum.CameraType.Custom
            Camera.CameraSubject = player.Character:WaitForChild("Humanoid")
            RunService:UnbindFromRenderStep("UpdateCamera")
            _G.isJumpScareActive = false
            -- Reset Atmosphere settings
            setAtmosphereSettings(initialDensity, initialColor, initialDecay, initialGlare, initialHaze)
            -- Disable the JumpScareStatic ScreenGui
            local gui = player:FindFirstChildOfClass("PlayerGui"):FindFirstChild("JumpScareStatic")
            if gui then
                gui.Enabled = false
            end
            -- Re-enable inventory, leaderboard, and chat bar
            StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, true)
            StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, true)
            StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
        end
    end

    RunService:BindToRenderStep("UpdateCamera", Enum.RenderPriority.Camera.Value, updateCamera)
end

local function spawnExistingDummy()
    local jumpScareFolder = ReplicatedStorage:FindFirstChild("JumpScare")
    if not jumpScareFolder then
        warn("JumpScare folder not found in ReplicatedStorage")
        return nil
    end

    local dummy = jumpScareFolder:FindFirstChild("Dummy")
    if not dummy then
        warn("Dummy not found in JumpScare folder")
        return nil
    end

    local clonedDummy = dummy:Clone()
    clonedDummy.Parent = Workspace

    -- Set the position and orientation of each part
    local function setPartPosition(part, position)
        part.Position = position
        part.Orientation = Vector3.new(-3.999, 178.998, 0.07)
    end

    setPartPosition(clonedDummy.Head, Vector3.new(3.15, 385.749, 93.454))
    setPartPosition(clonedDummy.Torso, Vector3.new(3.15, 384.253, 93.349))
    setPartPosition(clonedDummy["Right Arm"], Vector3.new(1.65, 384.254, 93.323))
    setPartPosition(clonedDummy["Left Arm"], Vector3.new(4.65, 384.251, 93.375))
    setPartPosition(clonedDummy["Right Leg"], Vector3.new(2.65, 382.258, 93.201))
    setPartPosition(clonedDummy["Left Leg"], Vector3.new(3.65, 382.257, 93.218))
    setPartPosition(clonedDummy.HumanoidRootPart, Vector3.new(3.15, 384.253, 93.349))

    clonedDummy:SetPrimaryPartCFrame(CFrame.new(Vector3.new(3.15, 384.253, 93.349)) * CFrame.Angles(math.rad(-3.999), math.rad(178.998), math.rad(0.07)))

    return clonedDummy
end

local function spawnAndSetJumpScareCam()
    local jumpScareFolder = ReplicatedStorage:FindFirstChild("JumpScare")
    if not jumpScareFolder then
        warn("JumpScare folder not found in ReplicatedStorage")
        return nil
    end

    local jumpScareCam = jumpScareFolder:FindFirstChild("JumpScareCam")
    if not jumpScareCam then
        warn("JumpScareCam not found in JumpScare folder")
        return nil
    end

    local clonedJumpScareCam = jumpScareCam:Clone()
    clonedJumpScareCam.Parent = Workspace

    _G.isJumpScareActive = true
    _G.setJumpScareActive(true)
    setCameraToJumpScareCam(clonedJumpScareCam)

    -- Set jump scare Atmosphere settings
    setAtmosphereSettings(jumpScareDensity, jumpScareColor, jumpScareDecay, jumpScareGlare, jumpScareHaze)

    -- Enable the JumpScareStatic ScreenGui
    local gui = player:FindFirstChildOfClass("PlayerGui"):FindFirstChild("JumpScareStatic")
    if gui then
        gui.Enabled = true
    end

    -- Play a random jump scare sound
    playRandomJumpScareSound()

    -- Play the static sound
    playJumpScareStaticSound()

    -- Hide inventory, leaderboard, and chat bar
    StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
    StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
    StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

    -- Spawn the existing dummy
    local dummy = spawnExistingDummy()

    return clonedJumpScareCam, dummy
end

local function makeHeadInvisible(character)
    local head = character:FindFirstChild("Head")
    if head then
        head.Transparency = 1
        for _, child in ipairs(head:GetChildren()) do
            if child:IsA("Decal") or child:IsA("FaceInstance") then
                child:Destroy()
            end
        end
    end
end

local function onPlayerDeath()
    -- Set jump scare Atmosphere settings immediately upon death
    setAtmosphereSettings(jumpScareDensity, jumpScareColor, jumpScareDecay, jumpScareGlare, jumpScareHaze)

    local jumpScareCam, dummy = spawnAndSetJumpScareCam()
    wait(10)
    if jumpScareCam then
        jumpScareCam:Destroy()
    end
    if dummy then
        dummy:Destroy()
    end

    -- Ensure the player respawns only once
    if not _G.isRespawning then
        _G.isRespawning = true
        respawnEvent:FireServer()
        wait(5) -- Wait for the respawn to complete
        _G.isRespawning = false
    end
end

local function initializeCharacter()
    local character = player.Character or player.CharacterAdded:Wait()
    while not character.Parent do
        wait()
    end
    local primaryPart = character:WaitForChild("HumanoidRootPart", 10)
    local humanoid = character:WaitForChild("Humanoid", 10)

    if primaryPart and humanoid then
        makeHeadInvisible(character)
        humanoid.Died:Connect(onPlayerDeath)
    end
end

player.CharacterAdded:Connect(initializeCharacter)
initializeCharacter()

-- Set initial Atmosphere settings
setAtmosphereSettings(initialDensity, initialColor, initialDecay, initialGlare, initialHaze)

respawnEvent.OnClientEvent:Connect(function()
    if player.Character then
        player.Character:BreakJoints()
    end
end)

this script is severely unfinished and needs multiple parts so i dont mind sharing but theres a folder in replicated storage that should spawn this r6 dummy model but it doesnt and i cant really find a work around i feel like this is simple im just not expanding my tiny brain to find the answer i would really appreciate any help alot :slight_smile:

this is a local script by the way its supposed to only work for a single client but i think its kinda obvious with the name jumpscare popping up in it

The issue may be from trying to move the dummy. You can use Model:MoveTo to move the entire dummy at the same time. If that doesn’t work, check the workspace to see if the dummy is even being parented correctly.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.