Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Help with my ice scripts that should allow you to fall through, crack, and play crack sound?

Asked by
Rfrizz 0
12 days ago

So I need help with this ice script. There is 3 scripts, the ice texture, the sound script, and the script for the ice you fall through

so for the sound of the ice breaking this was the script

 function onTouch(hit)
    script.Sound:Play()
end

This script had a sound in it btw but the sound didnt play

Then there was this Texture for the ice that was a crack and this was the non working script

function onTouch(hit)

wait(1)
script.Parent.Transparency = 0
end 
script.Parent.Touched:connect(onTouch)

The crack texture starts off as invisible and this script should make it visible

This is the script for falling through the ice and it worked but I might as well put it here

function onTouch(hit)

script.Parent.CanCollide = false 
script.Parent.Transparency = 1
wait (5)
script.Parent.CanCollide = true 
script.Parent.Transparency = 0 
end 
script.Parent.Touched:connect(onTouch)

1 answer

Log in to vote
0
Answered by
xPolarium 1264 Moderation Voter
11 days ago
Edited 11 days ago

You have the idea of how to use the Touched event yet you have multiple scripts with this event to run separate code when you can easily run it in one.


To start, you need to set up the Touched event to only allow player interaction. We can do this by looking for the Humanoid if the Parent of hit has one:

local function onTouch(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")

    if human then
        --code here runs only if we get a character touching
    end
end

script.Parent.Touched:Connect(onTouch) --Setup event connection

Simple and easy to read. We then follow the sequence to get a 'ice' breaking effect. This includes the CanCollide and Transparency properties as you had used. I'll also add a cooldown variable to know when to make the ice a solid again.

local iceBlock = script.Parent
local iceCooldown = 5

local sound = iceBlock.Sound --Make sure 'Sound' is a child of the BasePart.

local canTouch = false

local function onTouch(hit)
    local human = hit.Parent:FindFirstChild("Humanoid")

    if human then
        if canTouch then return end --this is a debounce
        canTouch = true

        iceBlock.Transparency = 1
        iceBlock.CanCollide = false
        sound:Play()

        wait(iceCooldown)

        iceBlock.Transparency = 0
        iceBlock.CanCollide = true

        canTouch = false
    end
end

iceBlock.Touched:Connect(onTouch) --Setup event connection

Notice how I included the Sound object. Make sure that this sound object is inside of the part so it can play in 3D space. This makes it so that the sound emits from the position of the part.

You also mention of trying to place a Texture (Decal) on the ice block. If that's the case then you can extend this by adding another wait before setting the Transparency to 1 and Instance.new() the Decal.

Let me know if this had helped you or you need something explained.

References:

Ad

Answer this question