#rayResult not identifying materials

26 messages · Page 1 of 1 (latest)

sly river
#

hi guys, I was trying to create a code so that if the shot hit a glass material part, the sound of the impact would play. Idk why isnt working, anybody know why?

    local isGlass = false
    local glass = game.Workspace.Baseplate:FindFirstChild("Glass")
    if glass then
        local glassSize = glass.Size
        local glassPosition = glass.Position
        if rayResult.material == glass and (rayResult.position - glassPosition).Magnitude <= (glassSize.Magnitude / 2 + rayRadius) then
            isGlass = true
        end
    end


    local glassSoundEffect1 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass1
    local glassSoundEffect2 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass2
    local glassSoundEffect3 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass3
    local glassSoundEffect4 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass4
    local glassSoundEffect5 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass5

    if isGlass then
        local glassRandomNumber = math.random(1, 5)
        if glassRandomNumber == 1 then
            glassSoundEffect1:Play()
        elseif glassRandomNumber == 2 then
            glassSoundEffect2:Play()
        elseif glassRandomNumber == 3 then
            glassSoundEffect3:Play()
        elseif glassRandomNumber == 4 then
            glassSoundEffect4:Play()
        elseif glassRandomNumber == 5 then
            glassSoundEffect5:Play()
        end
    end

im using the roblox fps template btw

dense lark
#

do

sly river
# dense lark if rayResult.material == glass.material

like this?

    local isGlass = false
    local glass = game.Workspace.Baseplate:FindFirstChild("Glass")
    if glass then
        local glassSize = glass.Size
        local glassPosition = glass.Position
        -- check if the ray intersects with glass
        if rayResult.material == glass.material then
            isGlass = true
        end
    end
    
    
    local glassSoundEffect1 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass1
    local glassSoundEffect2 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass2
    local glassSoundEffect3 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass3
    local glassSoundEffect4 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass4
    local glassSoundEffect5 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass5
    
    if isGlass then
        local glassRandomNumber = math.random(1, 5)
        if glassRandomNumber == 1 then
            glassSoundEffect1:Play()
        elseif glassRandomNumber == 2 then
            glassSoundEffect2:Play()
        elseif glassRandomNumber == 3 then
            glassSoundEffect3:Play()
        elseif glassRandomNumber == 4 then
            glassSoundEffect4:Play()
        elseif glassRandomNumber == 5 then
            glassSoundEffect5:Play()
        end
    end
sly river
#

i have to put the code in starterplayerscripts?

dense lark
#

Show me the actual full script

sly river
#

k

#

this script its the fps template one

#

i just added the glass script

dense lark
sly river
#

not working :\

viral spruceBOT
#

studio** You are now Level 1! **studio

sly river
sly river
# dense lark depends on the script tbh

this one works?

local part = script.Parent
local humanoid = part.Humanoid
local glassSound1 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass1
local glassSound2 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass2
local glassSound3 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass3
local glassSound4 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass4
local glassSound5 = game.Workspace.Sounds.MaterialBulletImpact.Glass.BulletGlass5

local oldHealth = humanoid.Health

humanoid.HealthChanged:Connect(function(newHealth)
if newHealth < oldHealth then
local randomNumber = math.random(1, 5)

    if randomNumber == 1 then
        glassSound1:Play()
    end
    
    if randomNumber == 2 then
        glassSound2:Play()
    end
    
    if randomNumber == 3 then
        glassSound3:Play()
    end
    
    if randomNumber == 4 then
        glassSound4:Play()
    end
    
    if randomNumber == 5 then
        glassSound5:Play()
    end
end

end)

dense lark
sly river
dense lark
# sly river value instead of what? im a beginner in roblox studio

I hope you learned something new about scripting! Remember to never give up when trying to learn a new skill, even if the process becomes frustrating. If you want to learn more about scripting on Roblox, take a look at some of my other videos. Thanks for watching!

Script: https://pastebin.com/raw/ezC3aAk3

Join the RoScripter Group: https://www...

▶ Play video
sly river
#

oh ik

#

but how can i change in thescript?

halcyon falcon
#

you reference the value instance name

#

for example the value instance name is hello

#

you change the value by doing: