Every time a player touches the part, it will play. This is regardless if the player even rotates their character on the part. I recommend using a debounce so it will only play every second or so depending on how long the audio is playing. You should also check to make sure that you’re actually getting a player. Use an if statement to check if the part’s parent has a Humanoid.
local sound = script.Parent.Bruh
local partsound = game.Workspace.Red.san
local part = script.Parent
local red = game.Workspace.Red
part.Touched:Connect(function(bruh)
local plr = game.Players:GetPlayerFromCharacter(bruh.Parent)
if plr then
sound:Play()
partsound:Stop()
partt.Transparency = 1
end
end)
red.Touched:Connect(function(part)
local plr = game.Players:GetPlayerFromCharacter(part.Parent)
if plr then
sound:Stop()
partsound:Play()
partt.Transparency = 0
end
end)
Issue with your script I noticed. Not sure if this is what you’re looking for as you didn’t explicitly state what’s wrong.
The issue with your script is that there’s no debounce. I’m going to simply add an additional conditional to your script to check if the sound is already playing.
local sound = script.Parent.Bruh
local partsound = game.Workspace.Red.san
local part = script.Parent
local red = game.Workspace.Red
part.Touched:Connect(function(bruh)
local plr = game.Players:GetPlayerFromCharacter(bruh.Parent)
if plr and sound.Playing == false then
sound:Play()
partsound:Stop()
partt.Transparency = 1
end
end)
red.Touched:Connect(function(part)
local plr = game.Players:GetPlayerFromCharacter(part.Parent)
if plr and partsound.Playing == false then
sound:Stop()
partsound:Play()
partt.Transparency = 0
end
end)
Another thing, every time you run your touched function, you are overwriting a previous variable part. On line 4 of your image, you specify the variable part as script.Parent. On line 16, you set part as the object that touched red.
Since the variable goes across the entire code, it’s recommended you do that. If you had the variable only defined in a function, it wouldn’t matter. But in your case, I recommend you change the variable name.
Here is the script. You has some variable mistakes, but it’s fixed. Good attempt and keep on learning! Tell me if anything does not work. It is important to mention that @chexburger and @8w_a are correct and you should use a debounce since it would keep looping when you touch the parts.
local sound = script.Parent.Bruh
local partsound = workspace.Red.san
local part = script.Parent
local red = workspace.Red
local Debounce1 = false
local Debounce2 = false
part.Touched:Connect(function(Object)
local plr = game.Players:GetPlayerFromCharacter(Object.Parent)
if plr then
if not Debounce1 then
Debounce1 = true
red.Transparency = 0
part.Transparency = 1
sound:Play()
partsound:Stop()
Debounce2 = false
end
end
end)
red.Touched:Connect(function(Hit)
local plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
if plr then
if not Debounce2 then
Debounce2 = true
part.Transparency = 0
red.Transparency = 1
sound:Stop()
partsound:Play()
Debounce1 = false
end
end
end)