I'm trying to make a board that breaks when you step on it. I want this part to play a cracking sound, then turn in visible and CanCollide off. I have a basic understanding of Roblox Lua so please excuse the badness of my code.
01 | function onTouched(part) |
02 |
03 | CrackSound.SoundId = "rbxassetid://131144461" |
04 | this.Touched:Connect( function (part) |
05 | if part.Parent:FindFirstChild( "Humanoid" ) and true then |
06 |
07 | end |
08 | BrickSound:Play() |
09 | transparency = 1 |
10 | CanCollide = false |
11 | anchored = false |
12 | end |
few things wrong with your code your code:
01 | function onTouched(part) -- no bad |
02 |
03 | CrackSound.SoundId = "rbxassetid://131144461" -- what is cracksound???? |
04 | this.Touched:Connect( function (part) -- do hit instead |
05 | if part.Parent:FindFirstChild( "Humanoid" ) and true then - |
06 |
07 | end |
08 | BrickSound:Play() |
09 | transparency = 1 |
10 | CanCollide = false |
11 | anchored = false |
12 | end |
edited code:
01 | local part = script.Parent |
02 | local CrackSound = Instance.new( "Sound" , part) |
03 | CrackSound.SoundId = "rbxassetid://131144461" |
04 | part.Touched:Connect( |
05 | function (hit) |
06 | if hit.Parent:FindFirstChild( "Humanoid" ) and true then |
07 | CrackSound:Play() |
08 | part.Transparency = 1 |
09 | part.CanCollide = false |
10 | part.Anchored = false |
11 | end |
12 | end |
13 | ) |
Explanation:
You did a few things wrong. function onTouched(part)
is not needed as you did this.Touched:Connect(function(part)
CrackSound.SoundId
I believe you forgot to reference that in the code, so I did script.Parent.
BrickSound
isn't CrackSound?
1 | transparency = 1 |
2 | CanCollide = false |
3 | anchored = false |
Remember, Roblox is case sensitive and these are children/properties of the part.
So first of all, you haven't defined the sound variable. Secondly you do not the and true
part, as it is unnecessary and just causes your script break (potentially.) Thirdly, you need to define the variables if you want to access the properties. Roblox introduced variables to make your life easier.
01 | local part = script.Parent |
02 | local crackSound = script.Parent.CrackSound |
03 | crackSound.SoundId = "rbxassetid://131144461" |
04 | function onTouched(hit) --Use a different parameter name in order to not confuse yourself. |
05 | if hit.Parent:FindFirstChild( "Humanoid" ) then --You do not need the 'and true' part. Unless if you're using a debounce. |
06 | crackSound:Play() |
07 | part.Transparency = 1 |
08 | part.CanCollide = false |
09 | part.Anchored = false |
10 | end |
11 | end |
12 |
13 | script.Parent:Connect(onTouched) --Make sure you connect the function after you defined it. |
(Note: I seen that you said crackSound is not a valid member of part
. That means you haven't put the CrackSound Sound as the child of the Part. Placement is key, and if you place it incorrectly, it may break your script, having an error, or not.)