Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Wrong sound played in script due to variable magically changing its value?[FIXED]

--Complex Combat Punch Script
--Created by SpyGuyTBM
--NOT FOR PUBLIC USE
--Version 0.1.2


local KD = 0
local click = tick()
local Com = 0
local up
local down
local waka
local succ 
userInput = game:GetService("UserInputService")


userInput.InputBegan:connect(function(input)
 if input.UserInputType == Enum.UserInputType.Keyboard and input.UserInputState == Enum.UserInputState.Begin and input.KeyCode == Enum.KeyCode.W then
up = true
wait(0.3)
up = false	
end
end)

userInput.InputBegan:connect(function(input)
 if input.UserInputType == Enum.UserInputType.Keyboard and input.UserInputState == Enum.UserInputState.Begin and input.KeyCode == Enum.KeyCode.S then
down = true
wait(0.3)
down = false	
end
end)


userInput.InputBegan:connect(function(input)
 if input.UserInputType == Enum.UserInputType.Keyboard and input.UserInputState == Enum.UserInputState.Begin and input.KeyCode == Enum.KeyCode.G then
 if game.Players.LocalPlayer.leaderstats.MoveActive.Value == 0 then
	
	if tick() - click > 0.5 then
                Com = 0
click = tick()
	        end
	
	if tick() - click < 0.5 then
                Com = Com + 1
click = tick()

	        end
	
	if Com > 3 then
		Com = 1
	end
	
	if up == true then
		Com = 0
	end
	if down == true then
		Com = 0.1
	end
if game.Players.LocalPlayer.leaderstats.MoveActive.Value == 0 then
game.Players.LocalPlayer.leaderstats.MoveActive.Value =1
game.Players.LocalPlayer.leaderstats.Stop.Value = 0
KD=1
succ = 0 -- The Criminal	

if Com == 0 then
	local animTrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.TestAnim3)
animTrack:Play()
	
end

if Com == 0.1 then
	local animTrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.ExportAnim)
animTrack:Play()
	
end

if Com >0 and Com<= 2 then
	local animTrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.TestAnim)
animTrack:Play()
	
end

if Com == 3 then
	local animTrack = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.TestAnim2)
animTrack:Play()
	
end

game.Players.LocalPlayer.Character["Left Arm"].Touched:connect(function(h)
	
	
if h.Parent:findFirstChild("Humanoid") and KD == 1 then
	-- Changes succ to 1
	succ = 1
		if Com == 0.1 then
				if h.Parent.Torso.CFrame.X - game.Players.LocalPlayer.Character.Torso.CFrame.X >= 0 then
				local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity = (Velocity*40) 
				h.Parent.Torso.Velocity = Velocity
			end
			if h.Parent.Torso.CFrame.X- game.Players.LocalPlayer.Character.Torso.CFrame.X <= 0 then
				local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity = (Velocity*40) 
				h.Parent.Torso.Velocity = Velocity
			end
 waka = 0.5

 waka = 0.5 

		end
		
		if Com == 0 then
			local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity =  Vector3.new(0,50,0)
h.Parent.Torso.Velocity = Velocity 
 waka = 0.5
		end
		


if Com >=0 and Com<=2 and Com~=0.1 then
local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity = (Velocity*8) + Vector3.new(0,10,0)
h.Parent.Torso.Velocity = Velocity 
 waka = 0.1
Velocity = Vector3.new(0,0,0)
end


if Com==3 then
local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity = (Velocity*40) + Vector3.new(0,40,0)
h.Parent.Torso.Velocity = Velocity 
 waka = 0.5
Velocity = Vector3.new(0,0,0)
end

end
	end)
	
	
	
	
game.Players.LocalPlayer.Character["Right Leg"].Touched:connect(function(h)
	
	
	if h.Parent:findFirstChild("Humanoid") and KD == 1 then
		-- Changes succ to 1
		succ = 1
		if Com == 0.1 then
				if h.Parent.Torso.CFrame.X - game.Players.LocalPlayer.Character.Torso.CFrame.X >= 0 then
				local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity = (Velocity*40) 
				h.Parent.Torso.Velocity = Velocity
			end
			if h.Parent.Torso.CFrame.X- game.Players.LocalPlayer.Character.Torso.CFrame.X <= 0 then
				local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity = (Velocity*40) 
				h.Parent.Torso.Velocity = Velocity
			end
 waka = 0.5
		end
		
		if Com == 0 then
			local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity =  Vector3.new(0,50,0)
h.Parent.Torso.Velocity = Velocity 
 waka = 0.5
		end
		


if Com >=0 and Com<=2 and Com~=0.1 then
local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity = (Velocity*8) + Vector3.new(0,10,0)
h.Parent.Torso.Velocity = Velocity 
 waka = 0.1
Velocity = Vector3.new(0,0,0)
end


if Com==3 then
local Velocity = game.Players.LocalPlayer.Character.Torso.CFrame.lookVector
Velocity = (Velocity*50) + Vector3.new(0,30,0)
h.Parent.Torso.Velocity = Velocity 
 waka = 0.5
Velocity = Vector3.new(0,0,0)
end

end
end)



if Com == 0.1 then
			
 waka = 0.5

		end
		
		if Com == 0 then
		
 waka = 0.5
		end
		


if Com >=0 and Com<=2 and Com ~=0.1 then

 waka = 0.1

end


if Com==3 then

waka = 0.5

end

-- Determines what sound to make

print("succ is".. succ)
if succ == 0 then
	script.Sound2:Play()
end

if succ == 1 then
	script.Sound:Play()
end


	

wait(waka)
game.Players.LocalPlayer.leaderstats.MoveActive.Value = 0
game.Players.LocalPlayer.leaderstats.Stop.Value = 1
KD = 0
end
end
end
end)

Lol, this is a long script.

So anyways, i have made a punch script that deals knockback and does diffrent things if difrrent keys are pressed together.

I am integrating sound, where if the punch makes no contact, it plays "Sound1" and if contact is made, it plays "sound2"

So, there is nothing wrong with the sounds themselves, rather the way the script determines what sound to play.

I have a variable called "succ" that does this job. (BTW it means succsess, not, you know)

At the beggining, it is set to "0" (no contact), then if it makes contact, it is set to "1", (contact). Then at the end of the script, it determines what sound to play.

For some reason, it always plays 0, even when contct is made. I know there is no problem defining the variable, because when i remove the line of code that sets the variable to 0, it will play sound2, but then it won't play sound1 (obviously). And i put a print to print what succ was right after succ was told to be 1, and it did become 1. it just magically becomes 0.

I can't figure out whats wrong.

Thanks in advance.

(BTW i put comments next to things that are important to this question. Comment if more information is needed)

1 answer

Instead of local succ, try changing it to succ = nil.

1 comment

0

I actually found the error eventually myself. What was happening is that the events fired later than when the code executed to operation of choosing what sound to make.

This discussion is preserved as it was. New answers, comments, and votes are closed.