What are you trying to achieve? I’m making a City Game, and I want cars to have alarms. How I’m trying to make this is that when a player is too close to a car, the Alarm sounds (looped). I want EVERYONE in the Server to be able to hear the alarm.
What is the issue? I tried some things from the Internet like ‘‘Play On Touch’’, the only Issue being that once I play the 2 second Audio (The Car alarm) It doesn’t repeat consistently. When I put it to looped it’s ALWAYS looped. I want it to play when touched, and when not touched, not playing,
What solutions have you tried so far? I’ve tried reaching out to the creators of the scripts, but they don’t respond (Ain’t a wonder, Eh?)
The code you see below is a script I found online, but as descrbied above, it does not loop and continues to play, when not touched.
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
script.Parent.Sound.Playing=true
end
end)
Did you have a read through this? It has scripts at the bottom for local and server sounds as well as a click to play/stop feature that you should be able to carry over to your idea.
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
script.Parent.Sound:Play()
end
end)
You should not play sounds via .Playing property but instead call :Play() method. Other than that I don’t really understand what you are trying to achieve other than a car alarm.
Sorry, I’m trying to achieve that a 2 second car alarm sound keeps playing as if looped, until the player leaves that certain area. (if it’s still a bad explanation please tell me)
I’d think a large cancollide false Part around the vehicle with a touched event to start the sound and then use a BasePart.TouchEnded section to turn the sound off.
Just make sure that whatever touches it has a Humanoid in it so it doesn’t touch walls or lamp posts and turn on.
Why not try cloning the sound then play it?
Serverscript:
local deb = false
script.Parent.Touched:connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') and not deb then
deb = true
local newSound = script.Parent.Sound:Clone()
newSound.Parent = script.Parent
newSound:Play()
task.wait(5) -- wait till you can play the sound again.
newSound:Destroy()
deb = false
end
end)
I wrote a Local script that will do what you asked for, I think at least…
--[[
Every player will have this localscript on their computer doing the checks so everybody will hear the alarm,
no need to strain the server for this task
]]--
-- Typical imports, self-explanatory...
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local targetPart = Workspace.AlarmArea -- A range part, a part to check the range from
local targetAlarm = targetPart.Alarm -- Your alarm sound
local range = 5 -- Range in studs
local function arePlayersInArea()
for _, player in pairs(Players:GetPlayers()) do -- Go through each player
local character = player.Character
if not character then
continue -- If their character does not exist skip check for them
end
local root = character.PrimaryPart
if not root then
continue -- If they don't have a primary part then skip the check
end
if (targetPart.Position - root.Position).Magnitude <= range then
-- If the player is within our range then...
return true -- Tell whoever called this function that there are in fact players in the area
end
end
end
while true do -- Keep checking, forever...
task.wait(0.5) -- Do not remove this unless you want to crash Roblox
local triggerAlarm = arePlayersInArea() -- Are there any players in the area?
if triggerAlarm and not targetAlarm.Playing then
-- Player(s) were found in the area and the alarm is not playing
targetAlarm:Play() -- Let's start the alarm then
elseif not triggerAlarm and targetAlarm.Playing then
-- No player(s) are in the area and the alarm is still playing
targetAlarm:Stop() -- Let's stop the alarm then
end
end
I don’t think “.Touched”/".TouchEnded" should be used for this use case. I think checking the magnitudes (distances) between every player’s character & every vehicle would work better, here’s an example script for you to take inspiration from.
local players = game:GetService("Players")
local run = game:GetService("RunService")
local vehicles = workspace.Vehicles --Example vehicle folder.
local alarm = workspace.Alarm --Example sound instance.
local debounce = false
run.Stepped:Connect(function()
if debounce then return end
for _, player in ipairs(players:GetPlayers()) do
local closestVehicle, closestDistance = nil, math.huge
for _, vehicle in ipairs(vehicles:GetChildren()) do
local distance = player:DistanceFromCharacter(vehicle.PrimaryPart.Position) --Make sure each vehicle model has its "PrimaryPart" property set.
if distance < closestDistance then
closestVehicle = vehicle
closestDistance = distance
end
end
if closestDistance <= 30 then --Closest vehicle to player's character is less than 30 seconds.
debounce = true
alarm:Play() --Play the alarm.
alarm.Stopped:Wait()
debounce = false
break --Stop loop immediately.
end
end
end)