So I made a script here inside this part. This script detects throughout the WHOLE game whether there are parts that are called “GLIDER” that exist. If these are found, then it runs the rest of the script. However if there are none it won’t do anything
Now if there ARE any gliders that exist. And any of these gliders happen to touch this part then it will make a sound.
Basically what I’m doing is trying to emulate the click-clack sound a train makes. If you don’t already know the clickety-clack sound comes from the wheels hitting the gaps between sections of railroad track. So thats what I am trying to emulate here
Heres my script, any CONSTRUCTIVE feedback is appreciated, and if this script isn’t right please let me know what I can fix, thanks.
local railJoint = script.Parent
local clickSound = script.Parent:WaitForChild("CLICK-CLACK")
local workSpace = game:GetService("Workspace")
local glidersExist = false
local function checkForGliders(descendant)
if descendant:IsA("BasePart") and descendant.Name == "GLIDER" then
local glidersExist = true
end
end
local descendants = workspace:GetDescendants()
for _, descendant in ipairs(descendants) do
checkForGliders(descendant)
end
if glidersExist then
print("There are gliders that exist in the workspace!")
function makeClickClack()
clickSound:Play()
end
else
print("No gliders have been found in the workspace.")
end
railJoint.Touched:Connect(makeClickClack)
Then you need to rewrite checkGliders func because it checks only when game is started. I mean you add another glider it wont detect it.
local railJoint = script.Parent
local clickSound = script.Parent:WaitForChild("CLICK-CLACK")
local workSpace = game:GetService("Workspace")
local function checkForGliders()
local gliders = {}
for _, v in ipairs(workspace:GetDescendants()) do
if v:IsA("BasePart") and v.Name == "GLIDER" then
table.insert(gliders, v)
end
end
return gliders
end
while task.wait(2) do
local gliders = checkForGliders()
if #gliders > 0 then
print("There are gliders that exist in the workspace!")
function makeClickClack()
clickSound:Play()
end
railJoint.Touched:Connect(makeClickClack)
else
print("No gliders have been found in the workspace.")
end
end
This is test code. Best way is to use tags from Collection service or make folder for gliders. I can make script for you but after 20 minutes. Better to use .ChildAdded to folder then while loop
New question. How much rail joint do you have?
Collection service is better from server. Wait, wait
Another question is sound need to be local? Like is it only sound?(if yes then code is very simple)
Edit: Also touched event is not trustworthy, do you want me to make another detecting system.
I found 3 solutions:
CollectionService. Used on server to detect railjoint/railjoints and gliders
Script that just check what touched it and if it is glider then it will make sound
Add script to the glider and when glider touches(or comes near) railjoint it make sound(pros of it that you can raycast to check what is under you or near(depends on using it))
Wow, if it’s that much. Why you won’t just play sound when glider is moving?
It will be easier, cheaper.(and you can add statement that sound plays only when glider reached desired speed). I don’t see downsides for this
Also, you can raycast every 0.1(or 0.2) seconds from glider downwards and if railJoint is not previus railjoint then the sound plays. I don’t really know how sound in roblox works.
Oh, I got an idea. You can use my solution
BUT
You will not play a loop for it, you will spawn a sound. Like if glider is moving it will leave sounds behind it.
I think this is more realistic, I don’t think “sounds” are expensive for server but for optimization you can add it to local script