I am coding a UI, which when a button is clicked performs several actions, in one of them, it would play a sound on the server.
The current parent of the sound is the replicated storage, the local script triggers a remote event and a script moves the sound to the workspace and plays it, but trying, its the only action that doesn’t work.
Function that fires the remote:
function l(se: Sound)
s.Parent.Rest:FireServer(se)
end
Script:
local s=script.Parent
local r=s.Rest
function ac(s: Sound)
s.Playing = true
s.Looped = true
s.Parent = workspace
while wait(.2) do
s.PlaybackSpeed=r:NextInteger(6, 1.1)
print(s.PlaybackSpeed)
end
end
r.OnServerEvent:Connect(function(pl: Player, s: Sound)
ac(s)
end)
function ac(s: Sound)
local newSound = s:Clone()
newSound.Parent = workspace
newSound.Playing = true
newSound.Looped = true
while wait(.2) do
s.PlaybackSpeed=r:NextInteger(6, 1.1)
print(s.PlaybackSpeed)
end
end
additionally, you could also shorten the last lines of code, as long as you add a player argument to your “ac()” function:
function ac(pl: Player, s: Sound)
--[code]
end
r.OnServerEvent:Connect(ac)
while task.wait(2) do
local lowestPitch = 0.8
local pitchAddition = 0.3
local randomPitch = lowestPitch + math.random() * pitchAddition
print(randomPitch) -- prints a random number between 0.8 and 1.1
-- set the playback speed of the sound here
end
You have a global variable s assigned as script.Parent and you’re also parsing an s Instance over to your function. This can generally interfere with variable scopes and references. Always helps to avoid that.
Additionally, where is the script located? Have you attempted debugging how much of the code in it’s bounds is ran and exactly where’s what may be going wrong?
Lastly, while there’s nothing wrong with using single-letter names for variables, it significantly reduces readability and can cause confusion as your code progresses in complexity. I initially thought s was the Sound instance you were attempting to relocate, and while you mentioned the Sound is in ReplicatedStorage, I presumably thought the script was parented to it until I looked at your function parameters.