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.
function onTouched(part) CrackSound.SoundId = "rbxassetid://131144461" this.Touched:Connect(function(part) if part.Parent:FindFirstChild("Humanoid") and true then end BrickSound:Play() transparency = 1 CanCollide = false anchored = false end
few things wrong with your code your code:
function onTouched(part) -- no bad CrackSound.SoundId = "rbxassetid://131144461" -- what is cracksound???? this.Touched:Connect(function(part) -- do hit instead if part.Parent:FindFirstChild("Humanoid") and true then - end BrickSound:Play() transparency = 1 CanCollide = false anchored = false end
edited code:
local part = script.Parent local CrackSound = Instance.new("Sound", part) CrackSound.SoundId = "rbxassetid://131144461" part.Touched:Connect( function(hit) if hit.Parent:FindFirstChild("Humanoid") and true then CrackSound:Play() part.Transparency = 1 part.CanCollide = false part.Anchored = false end end )
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?
transparency = 1 CanCollide = false 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.
local part = script.Parent local crackSound = script.Parent.CrackSound crackSound.SoundId = "rbxassetid://131144461" function onTouched(hit) --Use a different parameter name in order to not confuse yourself. if hit.Parent:FindFirstChild("Humanoid") then --You do not need the 'and true' part. Unless if you're using a debounce. crackSound:Play() part.Transparency = 1 part.CanCollide = false part.Anchored = false end end 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.)