Why wont the script work the first time?

  1. What do you want to achieve? A script that makes the play fly when they say the correct spell.

  2. What is the issue? The spell doesn’t work the first time, but when I stop flying and say the spell again it works.

  3. What solutions have you tried so far? I thought it might be because the flying = false was outside of the remote event so I placed it inside of the remote event but that just made things even worse.

Here’s my local script which handles the OnServerEvent:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")

local wPressed = false

local flying = false

game.ReplicatedStorage.ZatannaRemotes.Flight.OnClientEvent:Connect(function(player, value)
	if value == "Fly" then
			flying = true

			for _, v in pairs(char:GetDescendants()) do
				if v.Name == "Aura" then
					v.Enabled = true
				end
			end

			local AnimationTracks = char.Humanoid:GetPlayingAnimationTracks()
			for i, track in pairs (AnimationTracks) do
				track:Stop()
			end


			local bv = Instance.new("BodyVelocity", char.PrimaryPart)
			bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bv.Velocity = Vector3.new(0,20,0)
			bv.Name = "FlightForce"

			repeat wait(0.1) until flying == false
			bv:Destroy()

			uis.InputBegan:Connect(function(key)	
				if key.KeyCode == Enum.KeyCode.W then
					wPressed = true
				end
			end)

			uis.InputEnded:Connect(function(key)	
				if key.KeyCode == Enum.KeyCode.W then
					wPressed = false
				end
			end)

			while wait() do
				if flying then
					char.PrimaryPart:FindFirstChild("FlightForce").Velocity = Vector3.new(0,0,0)

					if wPressed then
						char.PrimaryPart:FindFirstChild("FlightForce").Velocity = cam.CFrame.LookVector * 50
					end
				else
					wait(1)
				end
			end
	elseif value == "Stop" then
		if flying == true then
			flying = false
			
			for _, v in pairs(char:GetDescendants()) do
				if v.Name == "Aura" then
					v.Enabled = false
				end
			end
		end
	end
end)

here’s the server script which handles the player’s message:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		Player.Chatted:Connect(function(msg)
			local character = Character:FindFirstChild("Character")
			if Character.CharacterValue.Value == "Zatanna" then
				local spell = string.lower(msg)
				if spell == "ekat thgilf" then
					game:GetService("ReplicatedStorage").ZatannaRemotes.Flight:FireAllClients(Player, "Fly")
					
				elseif spell == "pots" then
					game:GetService("ReplicatedStorage").ZatannaRemotes.Flight:FireAllClients(Player, "Stop")

				end
			end
		end)
	end)				
end)

all help would be appreciated

Maybe move this below the while wait() do loop?

--SERVER

local Players = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local Remotes = Replicated.ZatannaRemotes
local Flight = Remotes.Flight

Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local InnerCharacter = Character:FindFirstChild("Character")
		if InnerCharacter then
			local CharacterValue = InnerCharacter:FindFirstChild("CharacterValue")
			if CharacterValue then
				if CharacterValue.Value == "Zatanna" then
					local Move = ""
					if Message:lower() == "ekat thgilf" then
						Move = "Fly"
					elseif Message:lower() == "pots" then
						Move = "Stop"
					end
					Flight:FireAllClients(Player, Move)
				end
			end
		end
	end)		
end)
--LOCAL

local Run = game:GetService("RunService")
local UserInput = game:GetService("UserInputService")
local Replicated = game:GetService("ReplicatedStorage")
local Remotes = Replicated:WaitForChild("ZatannaRemotes")
local Flight = Remotes:WaitForChild("Flight")

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HRP = Character:WaitForChild("HumanoidRootPart")
local Camera = workspace.CurrentCamera

local Connection = nil
local Flying = false

Flight.OnClientEvent:Connect(function(Player, Move)
	if Move == "Fly" then
		if Flying then
			return
		end
		Flying = true

		for _, Child in ipairs(Character:GetDescendants()) do
			if Child.Name == "Aura" then
				Child.Enabled = true
			end
		end

		for _, Track in ipairs(Humanoid:GetPlayingAnimationTracks()) do
			Track:Stop()
		end

		local BodyVelocity = Instance.new("BodyVelocity")
		BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BodyVelocity.Velocity = Vector3.new(0, 0, 0)
		BodyVelocity.Name = "FlightForce"
		BodyVelocity.Parent = HRP
		
		Connection = UserInput.InputBegan:Connect(function(Key, Processed)	
			if Processed then
				return
			end
			
			BodyVelocity.Velocity = Camera.CFrame.LookVector * 50
		end)
		
	elseif Move == "Stop" then
		if not Flying then
			return
		end
		Flying = false
		
		if Connection then
			Connection:Disconnect()
			Connection = nil
		end
		
		local FlightForce = HRP:FindFirstChild("FlightForce")
		if FlightForce then
			FlightForce:Destroy()
		end
		
		for _, Child in ipairs(Character:GetDescendants()) do
			if Child.Name == "Aura" then
				Child.Enabled = false
			end
		end
	end
end)

Works just how I wanted it to, I just have one question. Does doing local Move = “” make a big difference when firing a value to the client?

No because on the listening side you’re performing the following checks:

if Move == "Fly" then
elseif Move == "Stop" then

If “Move” is “” when the client is fired then nothing will occur.

1 Like

Although that raises a good point, you probably don’t want to fire the client when it isn’t necessary to do so, that can be achieved with the following minor change to the server script.

if CharacterValue.Value == "Zatanna" then
	local Move = ""
	if Message:lower() == "ekat thgilf" then
		Move = "Fly"
	elseif Message:lower() == "pots" then
		Move = "Stop"
	end
	if Move ~= "" then
		Flight:FireAllClients(Player, Move)
	end
end

strange, I changed it to this and it stopped working altogether. I added prints after changing the Move value and firing and none of them printed when I said chatted the words

You may have pasted it in wrong or accidentally removed something which you shouldn’t have, just stick with the previous two scripts.

1 Like