Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

0
I copied another script which played a sound when touched, so that why there are random values. astonplep 32 — 4y
1
did it work? raid6n 2196 — 4y
0
You can use either a named function or an anonymous function. Let people decide. @raid6n Dovydas1118 1495 — 4y
0
it has an error "cracksound is not a valid member of part" astonplep 32 — 4y
View all comments (3 more)
1
you didnt really tell us where cracksound is located, so i cant do anything about that raid6n 2196 — 4y
0
idk what cracksound is... astonplep 32 — 4y
0
.... CrackSound.SoundId is in ur code raid6n 2196 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

0
it still has that error, didn't you fix that? astonplep 32 — 4y
0
Place your sound under the part. And your error will go away. Dovydas1118 1495 — 4y
0
The script isn't working at all, I added a sound under the part with the id in it. astonplep 32 — 4y
0
I'm not understanding this.... I'm sorry. astonplep 32 — 4y
View all comments (2 more)
0
I thought this would be simple: when touched by humanoid change some properties and playa sound. astonplep 32 — 4y
0
Did you name the sound CrackSound? Dovydas1118 1495 — 4y

Answer this question