I am making a city RP game and I want people to have to stop for a train. In most city RP games people take traffic laws and throw it in the trash. Also, if possible, is there a way to make the horn sound happen when the train reaches a certain point? does anyone have any help?
Well tell us what you have tried so far, and what scripts you have tried
Put the train in ServerStorage and add this script to ServerScriptService:
local Train = game.ServerStorage.TrainName
while wait(math.random(number, number)) do
Train:Clone().Parent = workspace
-- Move the train to the correct location
end
Okay, I’ll try that. I’ll see if it works.
Have you worked on making the train move? If not I could help you
I think I have an idea of how I’ll get it to move.
Ok well to get the horn sound put a sound in the train model, and put a part on the section of track you want the horn to sound to play when it gets to that place, name it PlayHornSound.
Put a script in the sound(which is in the train model) and have it reference the train model and when the model touches a part have It check if it’s name of the part PlayHornSound if it is true let it play the horn sound.
When I put the train in ServerStorage it disappears and is just gone. Is that supposed to happen?
Yes, as only things in Workspace are rendered. Rest assured that the train is safely on the server. The script I gave you (might as well give you a summary) does:
- References the train in ServerStorage
- Waits for a random amount of time
- Clones the train and puts it into th workspace, allowing players to see the train.
Oh, okay. It is my first time with a type of script like this since I have been working a ton on my flight game and flight training game.
You would need to reference the train clone with a varaible, else you wouldn’t be able to do much with the clone.
local train = Train:Clone()
train:SetPrimaryPartCFrame(ExampleCFrame)
train.Parent = workspace
Thanks! I was originally assuming that the train was in the correct place to begin with, but your script will account for any train position.
Generally I wouldnt advice detecting collision for fast moving things that will have a lot of contact with the track.
Personally I would check every few seconds if the position of the front of the train is within a set distance of said crossing or location.
if (TriggerPosition - Train.Part.Position).magnitude < 100 then
Sound:Play()
end
It is easy to do and does not strain the server
Thanks for catching my mistake, forgot the trains would be touching tracks,
Yes you would be better off using the trains position