[SOLVED] How to Change this Local Script to Server Side

fadeGoal = 0
fadeRate = 0.01

function updateFade()
	local current = fade.BackgroundTransparency
	if current < fadeGoal then
		fade.BackgroundTransparency = math.min(fadeGoal,current+fadeRate)
	elseif current > fadeGoal then
		fade.BackgroundTransparency = math.max(fadeGoal,current-fadeRate)
	else
		fade.BackgroundTransparency = fadeGoal
	end
end

player = game.Players.LocalPlayer
character = player.Character or player.CharacterAdded:wait()
humanoid = player.Character:WaitForChild("Humanoid")
rs = game:GetService("RunService")

fadeGoal = 1

humanoid.Died:connect(function()
	script.Parent.Parent.DiedSound:Play()
	wait(1)
	fadeGoal = 0
	player.CharacterAdded:wait()
	script.Parent.Parent.DiedSound:Stop()
	fadeGoal = 1
end)

rs.RenderStepped:connect(updateFade)
fade.Visible = true

You cant move the part that is RenderStepped to the server. You will need to keep that part client side and trigger it with a RemoteEvent.

I only want the Sound to be heard by all Players.

You can make a remote that just takes a Sound instance to play and plays it on the server.

client side

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("PlayDiedSound")

local fadeGoal = 0
local fadeRate = 0.01

function updateFade()
    local current = fade.BackgroundTransparency
    if current < fadeGoal then
        fade.BackgroundTransparency = math.min(fadeGoal, current + fadeRate)
    elseif current > fadeGoal then
        fade.BackgroundTransparency = math.max(fadeGoal, current - fadeRate)
    else
        fade.BackgroundTransparency = fadeGoal
    end
end

local rs = game:GetService("RunService")

fadeGoal = 1

remoteEvent.OnClientEvent:Connect(function()
    fadeGoal = 0
    wait(1)
    fadeGoal = 1
end)

rs.RenderStepped:Connect(updateFade)
fade.Visible = true

server side

local Players = game:GetService("Players")

local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "PlayDiedSound"
remoteEvent.Parent = ReplicatedStorage

local function onDied(player)
    local sound = Instance.new("Sound")
    sound.SoundId = "rbxassetid://YourSoundIdHere" -- Replace with the actual SoundId
    sound.Parent = player.Character or player.CharacterAdded:Wait()
    sound:Play()
    wait(1)
    sound:Stop()
    sound:Destroy()
end

remoteEvent.OnServerEvent:Connect(onDied)

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        player.Character:WaitForChild("Humanoid").Died:Connect(function()
            remoteEvent:FireClient(player)
        end)
    end)
end)

I ended up fixing the problem so yeah.

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