I’m using CollectionService in ServerScriptService, so inside the script is a car crash script, and using a touched event but at the end of it around the (TaggedPart.Touched:Connect(function(onTouch) ) it seems to not work, right as I run the code it errors it saying something is not a valid member of model (workspace.vehicle.vehicle name)
What is the issue? Include screenshots / videos if possible!
so I know I have to rewrite the ending part of the script to detect a child but what do i write on it to fix this?
what should be changed?
local TestCol = game:GetService("CollectionService")
local TaggedParts = TestCol:GetTagged("BreakSound")
for _, TaggedPart in pairs(TaggedParts) do
function onTouch(part)
wait(0)
if part.Name == "JointoBreak" or part.Name == "JointoBreak1" or part.Name == "JointoBreak2" then
print("it hit it")
local audioTable = {"rbxassetid://5758352244", "rbxassetid://5758383587", "rbxassetid://5758976108", "rbxassetid://5862714774", "rbxassetid://5862726500", "rbxassetid://5862748979", "rbxassetid://5862778687", "rbxassetid://5862780025", "rbxassetid://5862783154", "rbxassetid://5862785460","rbxassetid://1228439088"}
local randomId = audioTable[math.random(#audioTable)]
local LoudV = math.random(1,2)
TaggedPart.Hit_Sound.SoundId = (randomId)
TaggedPart.Hit_Sound.Playing = true
TaggedPart.Hit_Sound.Volume = LoudV
TaggedPart.Sparks.Enabled = true
print("playing ID",randomId)
wait(0.1)
TaggedPart.Sparks.Enabled = false
end
end
TaggedPart.Touched:Connect(function(onTouch)--this is the only part of the error, but everything else works fine just need a fix or a rewrite.
print("Crashed the car")
end)
end
If you want to detect when the entirety of your Model is Touched, you could create a invisible hitbox Part inside of your Model, that you use to detect collisions with .Touched.
The Model instance itself doesn’t have any physical properties. It’s just a container for parts, which can collide with the world. You need some sort of part inside the model that acts as a hit detector for the Model.
I have joint break as the collision that causes the crash and the cars are the roblox style so the cars are unanchor so idk how I would do hitboxes unless the joint breaks are the hitboxes
Try looping through the tagged parts and get the touch event
for i,v in ipairs(TaggedParts) do --loop through your Taggedparts
if v:IsA("BasePart") then -- we might have to check if this is a part(obviously)
v.Touched:Connect(yourfunction) -- this will run touch event for every part
end
end
local TestCol = game:GetService("CollectionService")
local TaggedParts = TestCol:GetTagged("BreakSound")
for _, TaggedPart in pairs(TaggedParts) do
function onTouch(part) << -- Function
wait(0)
if part.Name == "JointoBreak" or part.Name == "JointoBreak1" or part.Name == "JointoBreak2" then
print("it hit it")
local audioTable = {"rbxassetid://5758352244", "rbxassetid://5758383587", "rbxassetid://5758976108", "rbxassetid://5862714774", "rbxassetid://5862726500", "rbxassetid://5862748979", "rbxassetid://5862778687", "rbxassetid://5862780025", "rbxassetid://5862783154", "rbxassetid://5862785460","rbxassetid://1228439088"}
local randomId = audioTable[math.random(#audioTable)]
local LoudV = math.random(1,2)
TaggedPart.Hit_Sound.SoundId = (randomId)
TaggedPart.Hit_Sound.Playing = true
TaggedPart.Hit_Sound.Volume = LoudV
TaggedPart.Sparks.Enabled = true
print("playing ID",randomId)
wait(0.1)
TaggedPart.Sparks.Enabled = false
end
end
print("Crashed the car")
end)
end << -- End Of the function
TaggedPart.Touched:Connect(onTouch) << -- Call The Function
I think your problem is because you include the touch function in your ‘onTouch’ function
From your script put your onTouch outside the loop
function onTouch(part) -- you made this argument 'part' so keep this as it be dont leave it empty because we use this to call another variable same with your argument name 'part'
wait(0)-- idk why you put a 0 time its kinda useless
if part.Name == "JointoBreak" or part.Name == "JointoBreak1" or part.Name == "JointoBreak2" then
print("it hit it")
local audioTable = {"rbxassetid://5758352244", "rbxassetid://5758383587", "rbxassetid://5758976108", "rbxassetid://5862714774", "rbxassetid://5862726500", "rbxassetid://5862748979", "rbxassetid://5862778687", "rbxassetid://5862780025", "rbxassetid://5862783154", "rbxassetid://5862785460","rbxassetid://1228439088"}
local randomId = audioTable[math.random(#audioTable)]
local LoudV = math.random(1,2)
TaggedPart.Hit_Sound.SoundId = (randomId)
TaggedPart.Hit_Sound.Playing = true
TaggedPart.Hit_Sound.Volume = LoudV
TaggedPart.Sparks.Enabled = true
print("playing ID",randomId)
wait(0.1)
TaggedPart.Sparks.Enabled = false
end
end
--Now loop through the TaggedParts
for _, part in pairs(TaggedParts) do
if part:IsA("BasePart") then
part.Touched:Connect(onTouch)
print("Crashed the cat")
end)
end
end
sorry my bad the enclosing brackets isnt in the proper place
change it to this
for _, part in pairs(TaggedParts) do
if part:IsA("BasePart") then
part.Touched:Connect(onTouch) -- the ontouch already had the 'part' argument and now referenced as the "part" BasePart from the if statement so now its properties set inside the onTouch(part) function will be valid according to a normal BasePart's properties
print("Crashed the cat")
end
end
Your problem is you can’t have a Touched event for a model or anything But BaseParts so yeah…
I have try everyone’s code they give me but they don’t seem to work out as I wanted it to
but if none of the scripts are working that people are giving me then people can stop replying to this and I don’t want to waste anyone time if this is not working out.
but you can still reply and still give me scripts that could/could not work and will still try them.
It is most probably because you might have not assigned one.
Select the Model and go to it’s properties. Now click on the primary part property and select the main part that I told you to make.
You should know one thing while replying on the devforum, no one here is going to spoonfeed you everything, and you shouldn’t expect everything to work as perfect as you want it to without you trying to fine tune it.
To your problem:
The thing is, you can’t use .Touched on models. You can use it on BaseParts, means you can either reference the PrimaryPart if it exists for the model or a part inside the model that is like the Main part or something.