Playing a sound when variable true

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I wanna play a sound when a variable becomes false or true from a module script.

  2. What is the issue? I wanna change the module’s variable from false to true all that with a regular script in ServerScriptService. When that variable becomes true I wanna play a sound.

  3. What solutions have you tried so far? I have tried to look for solutions but I pretty much didn’t understand how to change a module variable AND getting the value.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Regular Script:

local rs = game:GetService("ReplicatedStorage")
local ts = game:GetService("TweenService")


local kbtween = TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)

local events = rs:WaitForChild("Events")
local hitboxEvent = events:WaitForChild("Hitbox")
local soundEvent = events:WaitForChild("SoundSlide")

function knockback(victim, character, power)
	local ehrp = victim.HumanoidRootPart
	local uhrp = character.HumanoidRootPart
	local att = Instance.new("Attachment", ehrp)
	local lv = Instance.new("LinearVelocity", att)

	lv.MaxForce = 9999999
	lv.Attachment0 = att

	local lookVector = uhrp.CFrame.LookVector
	lv.VectorVelocity = lookVector * power

	local tween = ts:Create(lv, kbtween, {VectorVelocity = lookVector * 0.1})
	tween:Play()
	game.Debris:AddItem(att, 0.3)
end

function newHitbox(character, size, offset, damage, linger, kb)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp == nil then return end
	local weld = Instance.new("WeldConstraint", hrp)
	local hitbox = Instance.new("Part")
	
	weld.Part0 = hrp
	weld.Part1 = hitbox

	hitbox.Transparency = 1
	hitbox.CanCollide = false
	hitbox.CanQuery = false
	hitbox.Massless = true

	hitbox.Size = size
	hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector * offset.X + Vector3.new(0,offset.Y)
	hitbox.Parent = character
	
	hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") == nil then return end

		for _, v in pairs(hitbox:GetChildren()) do
			if v:IsA("ObjectValue") then
				if v.Value == hit.Parent then return end
			end
		end

		local hitCounter = Instance.new("ObjectValue", hitbox)
		hitCounter.Value = hit.Parent

		hit.Parent.Humanoid:TakeDamage(damage)

		if kb <= 0 then return end
		knockback(hit.Parent, character, kb)
	end)
	game.Debris:AddItem(hitbox, linger)
end

hitboxEvent.OnServerEvent:Connect(function(plr, size, offset, damage, linger, kb)
	newHitbox(plr.Character, size, offset, damage, linger, kb)
end)

Module Script:

local ModuleScript = {}

return ModuleScript

Sorry for the inconvenience,
Alexis

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

you can just do


--In the module script you could just make a function like

function module.PlaySound()

 if module.VariableName == True then
  playSound
 end

end

-- Now in a normal script do

local Module = require("Module script instance here")

Module.VariableName = Boolean

Module.PlaySound()


2 Likes

module scripts are quite interesting to use

for this example you can use

local ModuleScript = {}

ModuleScript.SoundEnabled = false 

return ModuleScript
local ModuleScript = require(workspace.ModuleScript)

if ModuleScript.SoundEnabled then -- 
     print("It's enabled")
else
      print("It's not enabled")
end
2 Likes

Hmm, I see, thanks for the help both of you, but sadly I cannot mark as solution both of you. Have a good day! :smile: :wave:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.