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

Pressing a key that is not included in script and spams particles that should be spawned only once?

I've made a script that basically gives me two different abilities and modes when I press Z. It works at all, but when I press another key that isn't Z it spams the particles that appear in each mode. Can anyone recognize the reason?

local m = game.Players.LocalPlayer:GetMouse()
local character = game.Players.LocalPlayer.Character
local fire = "nou"
local ice = "nou"

db = true
m.KeyDown:connect(function(k)
k = k:lower()
if k == 'z' then
	if script.Mode.Value == "" then
		script.Mode.Value = "Ice"
	elseif script.Mode.Value == "Ice" then
		script.Mode.Value = "Fire"
	elseif script.Mode.Value == "Fire" then
		script.Mode.Value = ""
	end
end


if script.Mode.Value == "Ice" then
	--Attiva una variante
    ice = "activated"

	--Qui modifichi le vesti - Modify old clothes
	script.Fire:Clone().Parent = character.LeftFoot
	script.Fire:Clone().Parent = character.LeftHand
	script.Fire:Clone().Parent = character.LeftUpperLeg
	script.Fire:Clone().Parent = character.LeftUpperArm
	
	
	--Rimuovi vecchie particelle - Remove old particles
	if fire == "activated" then
	character.RightFoot.Sparks:Destroy()
	character.RightHand.Sparks:Destroy()
    character.RightUpperLeg.Sparks:Destroy()
	character.RightUpperArm.Sparks:Destroy()
	character.RightFoot.Flame:Destroy()
	character.RightHand.Flame:Destroy()
    character.RightUpperLeg.Flame:Destroy()
	character.RightUpperArm.Flame:Destroy()
	end
	-- Con questo attivi le mosse - Here you activate the abilities
	print("Ice")
	script.Parent.IceX.Disabled = false
	script.Parent.FireX.Disabled = true
	
	elseif script.Mode.Value == "Fire" then
	-- Attiva una variante / N/A
	fire = "activated"
	--Qui modifichi le vesti - Here you modify clothes
	script.Flame:Clone().Parent = character.RightFoot
	script.Flame:Clone().Parent = character.RightHand
	script.Flame:Clone().Parent = character.RightUpperLeg
	script.Flame:Clone().Parent = character.RightUpperArm
	script.Sparks:Clone().Parent = character.RightFoot
	script.Sparks:Clone().Parent = character.RightHand
	script.Sparks:Clone().Parent = character.RightUpperLeg
	script.Sparks:Clone().Parent = character.RightUpperArm
	
	--Rimuovi vecchie particelle - Remove old particles
	if ice == "activated" then
	character.LeftFoot.Fire:Destroy()
	character.LeftHand.Fire:Destroy()
	character.LeftUpperLeg.Fire:Destroy()
	character.LeftUpperArm.Fire:Destroy()
	end
	
	-- Con questo attivi le mosse - Active the abilities
	print("Fire")
	script.Parent.IceX.Disabled = true
	script.Parent.FireX.Disabled = false
	
    elseif script.Mode.Value == "" then 
    
    -- Qui modifichi le vesti / Here you modify the clothes
    if fire == "activated" then
	character.RightFoot.Sparks:Destroy()
	character.RightHand.Sparks:Destroy()
    character.RightUpperLeg.Sparks:Destroy()
	character.RightUpperArm.Sparks:Destroy()
	character.RightFoot.Flame:Destroy()
	character.RightHand.Flame:Destroy()
    character.RightUpperLeg.Flame:Destroy()
	character.RightUpperArm.Flame:Destroy()
	
	print("Test")
	
	-- Qui togli le mosse - You deactivate the abilities
	script.Parent.IceX.Disabled = true
	script.Parent.FireX.Disabled = true
end
end
end)

1 comment

0

KeyDown is deprecated so you should use UserInputServiceInstead.

2 answers

As I mentioned in the comment, using KeyDown for keyboard input is deprecated so you should look into using UserInputService.

Anyways it's not what actually is causing the problem.

The problem you have is that you're cloning and parenting particles every time a KeyDown event is fired. That is happening no matter what key you press because you put it outside the if statement.

So your solution to this would be to just copy the code after the if statement from the function inside that if statement where you're checking if 'z' was pressed.

1 comment

0

Not working, I actually posted the script.

I tried to write so, but still didn't work.

local m = game.Players.LocalPlayer:GetMouse()
local character = game.Players.LocalPlayer.Character
local fire = "nou"
local ice = "nou"

db = true
m.KeyDown:connect(function(k)
k = k:lower()
if k == 'z' then
	if script.Mode.Value == "" then
		script.Mode.Value = "Ice"
	elseif script.Mode.Value == "Ice" then
		script.Mode.Value = "Fire"
	elseif script.Mode.Value == "Fire" then
		script.Mode.Value = ""
	 
    if db == true then
    if script.Mode.Value == "Ice" then
	--Attiva una variante
    ice = "activated"

	--Qui modifichi le vesti - Modify old clothes
	script.Fire:Clone().Parent = character.LeftFoot
	script.Fire:Clone().Parent = character.LeftHand
	script.Fire:Clone().Parent = character.LeftUpperLeg
	script.Fire:Clone().Parent = character.LeftUpperArm
	
	
	--Rimuovi vecchie particelle - Remove old particles
	 if fire == "activated" then
	  character.RightFoot.Sparks:Destroy()
	  character.RightHand.Sparks:Destroy()
      character.RightUpperLeg.Sparks:Destroy()
	  character.RightUpperArm.Sparks:Destroy()
	  character.RightFoot.Flame:Destroy()
	  character.RightHand.Flame:Destroy()
      character.RightUpperLeg.Flame:Destroy()
	  character.RightUpperArm.Flame:Destroy()
	-- Con questo attivi le mosse - Here you activate the abilities
	print("Ice")
	script.Parent.IceX.Disabled = false
	script.Parent.FireX.Disabled = true
	
	 elseif script.Mode.Value == "Fire" then
	-- Attiva una variante / N/A
	  fire = "activated"
	--Qui modifichi le vesti - Here you modify clothes
	  script.Flame:Clone().Parent = character.RightFoot
	  script.Flame:Clone().Parent = character.RightHand
	  script.Flame:Clone().Parent = character.RightUpperLeg
	  script.Flame:Clone().Parent = character.RightUpperArm
	  script.Sparks:Clone().Parent = character.RightFoot
	  script.Sparks:Clone().Parent = character.RightHand
	  script.Sparks:Clone().Parent = character.RightUpperLeg
	  script.Sparks:Clone().Parent = character.RightUpperArm
	
	--Rimuovi vecchie particelle - Remove old particles
	  if ice == "activated" then
	   character.LeftFoot.Fire:Destroy()
	   character.LeftHand.Fire:Destroy()
	   character.LeftUpperLeg.Fire:Destroy()
	   character.LeftUpperArm.Fire:Destroy()
	
	-- Con questo attivi le mosse - Active the abilities
	print("Fire")
	script.Parent.IceX.Disabled = true
	script.Parent.FireX.Disabled = false
	end
	end
end
end
end
end
end)

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