Help with whistle script

Hello there, today I was making a train when I realized the script to make the train whistle sound go was not making it play I have tried to fix it but I have no idea how to.

If Whistle is a sound than you can just do

local whistle = script.Whistle
Whistle:Play()
1 Like

I dont see anything that makes the sound :Play(), could you show the Local script code because the sound has something to do with ObjectValues

If your playing this from a LocalScript in the Workspace, you aren’t going to hear it, you need to change this to a server script. If you are running things that only work from a LocalScript you should utilise RemoteEvents.

–MADE BY NWSPACEK

–local sounds are fun, aren’t they!

local seat = script:WaitForChild(“seat”).Value
local Whistle = script:WaitForChild(“Whistle”).Value
local Piston = script:WaitForChild(“Piston”).Value
local Rotator = script:WaitForChild(“Rotator”).Value

function CreateLocalPart()
local LocalPart = Instance.new(“Part”,workspace.CurrentCamera)
LocalPart.Size = Vector3.new()
LocalPart.Transparency = 1
LocalPart.CanCollide = false
LocalPart.Anchored = true
return LocalPart
end

local LocalPiston = CreateLocalPart()
local LocalWhistle = CreateLocalPart()

local WhistleSound = Whistle.Sound:Clone()
WhistleSound.Parent = LocalWhistle

local stepped = game:GetService(“RunService”).RenderStepped
local cf = CFrame.new
local C2OS = cf().toObjectSpace
local comps = cf().components

local sfxs = {}

for i=1,3 do
for _,k in pairs (Piston:GetChildren()) do
if k:IsA(“Sound”) then
k:Clone().Parent = LocalPiston
table.insert(sfxs,k)
else
k:Destroy()
end
end
end

local LastPistonPosition = Piston.Position

local ,,,,,,xy,,zy,,, = comps(C2OS(Piston.CFrame,Rotator.CFrame))
local LastXPosition = xy
local LastZPosition = zy

local ShouldPlaySFX = false

local lastSfx = 1

function playsfx()
lastSfx = lastSfx+1
if lastSfx > #sfxs then
lastSfx = 1
end
sfxs[lastSfx].Pitch = math.random()*0.1+2.45
sfxs[lastSfx]:Play()
end

local WhistleOn = false
local WhistleDebounce = false
local WhistleAttackTime = 0.2
local WhistleDecayTime = 0.4
local WhistleFinalVolume = 0.5
function WhistleIt()
if WhistleDebounce then return end
WhistleDebounce = true
WhistleSound.Volume = 0
WhistleSound:Play()
local t = 0
local Decaying = false
repeat
if t <=WhistleAttackTime and WhistleOn and not Decaying then
local g = t/WhistleAttackTime
WhistleSound.Volume = ggWhistleFinalVolume
elseif t > WhistleAttackTime and WhistleOn and not Decaying then
WhistleSound.Volume = WhistleFinalVolume
elseif Decaying and WhistleOn then
Decaying = false
t = 0
elseif Decaying then
local g = t/WhistleDecayTime
WhistleSound.Volume = (WhistleDecayTime-g)*WhistleFinalVolume
elseif WhistleOn == false and not Decaying then
t = 0
Decaying = true
end
t = t+stepped:wait()
until not WhistleOn and Decaying and t > WhistleDecayTime
WhistleSound:Stop()
WhistleDebounce = false
end

Whistle.WhistleOn.Changed:connect(function(state)
if type(state) == “boolean” then
WhistleOn = state
if WhistleOn == true then
WhistleIt()
end
elseif type == “Parent” then
LocalWhistle:Destroy()
script:Destroy()
end
end)

while true do
if not Piston.Parent or not LocalPiston.Parent then
LocalPiston:Destroy()
LocalWhistle:Destroy()
script:Destroy()
break
end
if Rotator.RotVelocity.Magnitude > 0.1 or (Piston.Position-LastPistonPosition).magnitude > 0.2 then
LocalPiston.CFrame = Piston:GetRenderCFrame()
LocalWhistle.CFrame = Whistle:GetRenderCFrame()
LocalPiston.Velocity = Piston.Velocity
LocalWhistle.Velocity = Whistle.Velocity
ShouldPlaySFX = seat.Throttle ~= 0
local ,,,,,,xy,,zy,,, = comps(C2OS(Piston.CFrame,Rotator.CFrame))
if LastXPositionLastXPosition < 0.8 and xyxy >= 0.8 and ShouldPlaySFX then
playsfx()
ShouldPlaySFX = false
end
if LastZPositionLastZPosition < 0.8 and zyzy >= 0.8 and ShouldPlaySFX then
playsfx()
ShouldPlaySFX = false
end
LastXPosition,LastZPosition = xy,zy
end
LastPistonPosition = Piston.Position
stepped:wait()
end