Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a script to change some values in a part?

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

2 answers

✓ Accepted answer

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.

7 comments

0

I copied another script which played a sound when touched, so that why there are random values.

1

did it work?

0

You can use either a named function or an anonymous function. Let people decide. @raid6n

0

it has an error "cracksound is not a valid member of part"

1

you didnt really tell us where cracksound is located, so i cant do anything about that

0

idk what cracksound is...

0

.... CrackSound.SoundId is in ur code

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.)

6 comments

0

it still has that error, didn't you fix that?

0

Place your sound under the part. And your error will go away.

0

The script isn't working at all, I added a sound under the part with the id in it.

0

I'm not understanding this.... I'm sorry.

0

I thought this would be simple: when touched by humanoid change some properties and playa sound.

0

Did you name the sound CrackSound?

This discussion is preserved as it was. New answers, comments, and votes are closed.