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

Why does my fire spell still work after unequipping the wand?

I'm trying to make it where a fire spell only works if you have the wand equipped on your hand. However, after equipping and then unequipping, you still can use the fire spell. Does anyone have a fix for this?

Local Script

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local debounce = false

script.Parent.Equipped:Connect(function(player)
	UIS.InputBegan:Connect(function(Input, IsTyping)
		if not IsTyping then
			if not debounce then
				debounce = true
				
				if Input.KeyCode == Enum.KeyCode.Z then
					game.ReplicatedStorage.Fireball:FireServer(mouse.Hit.Position)
				end
				
				wait(0.1)
				debounce = false		
			end
		end
	end)
end)

Script

local fireball = game.ServerStorage:WaitForChild("Fireball")
script.Parent.Equipped:Connect(function()
	game.ReplicatedStorage.Fireball.OnServerEvent:Connect(function(player, mousePos)
		
		local character = player.Character
		local newFireball = fireball:Clone()
			
		newFireball.Position = character.HumanoidRootPart.Position
		newFireball.Parent = workspace
		local velocity = Instance.new("BodyVelocity", newFireball)
		velocity.Velocity = CFrame.new(newFireball.Position, mousePos).LookVector * 100
		velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			
		local touchConn
		
		touchConn = newFireball.Touched:Connect(function(hit)
			if hit == nil then return end
			if hit.Parent.Name == player.Name then return end
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent:FindFirstChild("Humanoid"):TakeDamage(20)
				if touchConn ~= nil then touchConn:Disconnect() end
				newFireball:Destroy()
				
			end
		end)
		
		wait(2)
		
		if touchConn ~= nil then touchConn:Disconnect() end
		newFireball:Destroy()
	end)
end)

7 comments

0

Hello, I am here to help you out.

0

Thanks!

0

Maybe you should watch Alvinblox's video of the fireball?

0

This is different. Fireball works, its just that I only want the fireball to cast when I have the wand only equipped.

0

Is their any errors?

0

Just going to say that mouse can be obtained as an argument passed by the Equipped event of a tool

0

no errors.

1 answer

✓ Accepted answer

You have to :Disconnect() your input connection that you made for your Z key.

Like so:

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()
local debounce = false

	local INPUT_CONNECTION

script.Parent.Equipped:Connect(function(player)
    INPUT_CONNECTION = UIS.InputBegan:Connect(function(Input, IsTyping)
        if not IsTyping then
            if not debounce then
                debounce = true

                if Input.KeyCode == Enum.KeyCode.Z then
                    game.ReplicatedStorage.Fireball:FireServer(mouse.Hit.Position)
                end

                wait(0.1)
                debounce = false        
            end
        end
    end)
end)

script.Parent.Unequipped:Connect(function(player)
	INPUT_CONNECTION:Disconnect()
end)

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