Cloned script does not work

Yo, guys, why a script does not work when it’s cloned? Here is the script that’s clone:

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local flyEvent = ReplicatedStorage.Events.flyEvent
local flyScript = game:WaitForChild("ServerStorage").FlyScript:Clone()

flyEvent.OnServerEvent:Connect(function()
	flyScript.Parent = game.StarterPlayer.StarterCharacterScripts
end)

I tried the script inside of “StarterCharacterScripts” and its works but when I clone it from ServerStorage to “StarterCharacterScripts” using a server script somehow it does not work

flyScript.Parent = game.StarterPlayer.StarterCharacterScripts:Clone()

error

StarterPlayer.StarterCharacterScripts cannot be cloned

Try disabling then re-enabling the cloned script.
And also clone it from the event, so it can run multiple times instead of once.

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local flyEvent = ReplicatedStorage.Events.flyEvent
local flyScript = game:WaitForChild("ServerStorage").FlyScript

flyEvent.OnServerEvent:Connect(function()
	flyScript:Clone().Parent = game.StarterPlayer.StarterCharacterScripts
    flyScript.Disabled = true
    wait(0.1)
    flyScript.Disabled = false
end)

flyScript.Parent:Clone().Parent = game.StarterCharacterScripts

It does clone but when cloned it does not active or work

admins = {1750930982,2916311109,2859393685}

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local flyEvent = ReplicatedStorage.Events.flyEvent
local flyScript = game:WaitForChild("ServerStorage").FlyScript

flyEvent.OnServerEvent:Connect(function(player)
	if table.find(admins, player.UserId) then
		flyScript:Clone().Parent = game.StarterPlayer.StarterCharacterScripts
		flyScript.Enabled = false
		wait(0.1)
		flyScript.Enabled = true
	end
end)

Do you want the script to run instantly when cloned?

Is the original fly script a local script by any chance?

Yes, so what I’m making is an admin menu, and inside this menu there is an enable fly button. I want to make it so when you click enable, it will clone the fly script.

Yes, it’s a local script that’s cloned from server storage to StarterCharacterScripts. but when I clone it, it does not work.

Accident.

Where is the original script you sent located and is it a local script or a server script?

By the way, you should make the cloned script a server script, or else you will only be able to fly from the client, so in the server you won’t actually be flying.

I also think that you should put the cloned script into the actual player’s character. I’m sure that scripts in
StarterCharacterScripts won’t run there, since it only sends the script for a player that joins.

The actual script works when I press “E”; it makes me fly, but everyone in the game can press “E,” so I want it only for the admin guy. If the admin guy presses the enable button, it will enable the script for the admin guy, so he is the only one who can press “E” and fly

Check where the script stops and tell me.
Use print() on each state of the code.
Check your output as well.

Nevermind I have a better Idea I can let the script inside StarterCharacterScript and make a remote event when I click the GUI button a remote event will be sent to the fly script and then I can,

but look when I send the event its do but it does not receive

19:51:58.146 ▶ flyEvent Sent (x1) - Client - Enable Script:6

local script for the GUI TextButton

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")

local flyEvent = ReplicatedStorage.Events.flyEvent

script.Parent.MouseButton1Down:Connect(function()

flyEvent:FireServer()

print("flyEvent Sent")

game.SoundService.menuClick:Play()

end)

local script for the fly

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local flyEvent = ReplicatedStorage.Events.flyEvent
local HoverAnimID = "rbxassetid://12596344348"
local FlyAnimID = "rbxassetid://12596341898"
local WindSoundEnabled = true

local BodyVelocity = script:WaitForChild("BodyVelocity"):Clone()
local v3 = script.BodyGyro:Clone()
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid") or Character:WaitForChild("Humanoid")
BodyVelocity.Parent = Character
v3.Parent = Character
local Hover = Instance.new("Animation")
Hover.AnimationId = HoverAnimID
local Fly = Instance.new("Animation")
Fly.AnimationId = FlyAnimID
local Sound1 = Instance.new("Sound", Character.HumanoidRootPart)
Sound1.SoundId = "rbxassetid://3308152153"
Sound1.Name = "Sound1"
if WindSoundEnabled == false then
	Sound1.Volume = 0
end
local v10 = Humanoid.Animator:LoadAnimation(Hover)
local v11 = Humanoid.Animator:LoadAnimation(Fly)
local Camera = game.Workspace.Camera
local function u2()
	if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
		return Humanoid.MoveDirection
	end
	local v12 = (Camera.CFrame * CFrame.new((CFrame.new(Camera.CFrame.p, Camera.CFrame.p + Vector3.new(Camera.CFrame.lookVector.x, 0, Camera.CFrame.lookVector.z)):VectorToObjectSpace(Humanoid.MoveDirection)))).p - Camera.CFrame.p;
	if v12 == Vector3.new() then
		return v12
	end
	return v12.unit
end
local Flymoving = script.Flymoving
local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local Flying = false

game:GetService("RunService").RenderStepped:Connect(function()
	if script.Parent == Character then
		if Flying == true then
			Humanoid:ChangeState(6)
			v3.CFrame = game.Workspace.Camera.CFrame
			if u2() == Vector3.new(0, 0, 0) then
				Flymoving.Value = false
			else
				Flymoving.Value = true
			end
			TweenService:Create(BodyVelocity, TweenInfo.new(0.3), {Velocity = u2() * 350}):Play()
		end

	end
end)

Flymoving.Changed:Connect(function(p1)
	if p1 == true then
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 100}):Play()
		v10:Stop()
		Sound1:Play()
		v11:Play()
		return
	end
	if p1 == false then
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 70}):Play()
		v11:Stop()
		v10:Play()
		Sound1.Volume = 0.4
		wait(0.05)
		Sound1.Volume = 0.35
		wait(0.05)
		Sound1.Volume = 0.3
		wait(0.05)
		Sound1.Volume = 0.25
		wait(0.05)
		Sound1.Volume = 0.2
		wait(0.05)
		Sound1.Volume = 0.15
		wait(0.05)
		Sound1.Volume = 0.1
		wait(0.05)
		Sound1.Volume = 0
		wait(0.02)
		Sound1:Stop()
		Sound1.Volume = 0.5
	end
end)

local xractivated = false


flyEvent.OnClientEvent:Connect(function(gameProcessed)
	print("flyEvent Recived")
	if gameProcessed then return end
	if Flying == false then
		Flying = true
		if Character:FindFirstChild("HumanoidRootPart") then
			v10:Play(0.1, 1, 1)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, false)
			Character.HumanoidRootPart.Running.Volume = 0
			Humanoid:ChangeState(6)
			BodyVelocity.Parent = Character.HumanoidRootPart
			v3.Parent = Character.HumanoidRootPart
		end
	else
		Flying = false
		Flymoving.Value = false
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, true)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, true)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, true)
		Character.HumanoidRootPart.Running.Volume = 0.65
		Humanoid:ChangeState(8)
		BodyVelocity.Parent = Character
		v3.Parent = Character
		v10:Stop()
		v11:Stop()
	end
end)

Line 93 - 108

flyEvent.OnClientEvent:Connect(function(gameProcessed)
	print("flyEvent Recived")
	if gameProcessed then return end
	if Flying == false then
		Flying = true
		if Character:FindFirstChild("HumanoidRootPart") then
			v10:Play(0.1, 1, 1)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, false)
			Character.HumanoidRootPart.Running.Volume = 0
			Humanoid:ChangeState(6)
			BodyVelocity.Parent = Character.HumanoidRootPart
			v3.Parent = Character.HumanoidRootPart
		end

where is the local script located?

If you see line 94, I have

print("flyEvent Recived")

but when I click the GUI button, it does send the message “flyEvent Sent”:

19:51:58.146   ▶ flyEvent Sent (x1)  -  Client - Enable Script:6

but it does not send the message “flyEvent Received” which means it sends fine but does not receive.

For the scripts, they are both a local script

the first script inside the text button

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local flyEvent = ReplicatedStorage.Events.flyEvent

script.Parent.MouseButton1Down:Connect(function()
	flyEvent:FireServer()
	print("flyEvent Sent")
	game.SoundService.menuClick:Play()
end)

the second script inside StarterCharacterScript:

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local flyEvent = ReplicatedStorage.Events.flyEvent
local HoverAnimID = "rbxassetid://12596344348"
local FlyAnimID = "rbxassetid://12596341898"
local WindSoundEnabled = true

local BodyVelocity = script:WaitForChild("BodyVelocity"):Clone()
local v3 = script.BodyGyro:Clone()
local Character = script.Parent
local Humanoid = Character:FindFirstChild("Humanoid") or Character:WaitForChild("Humanoid")
BodyVelocity.Parent = Character
v3.Parent = Character
local Hover = Instance.new("Animation")
Hover.AnimationId = HoverAnimID
local Fly = Instance.new("Animation")
Fly.AnimationId = FlyAnimID
local Sound1 = Instance.new("Sound", Character.HumanoidRootPart)
Sound1.SoundId = "rbxassetid://3308152153"
Sound1.Name = "Sound1"
if WindSoundEnabled == false then
	Sound1.Volume = 0
end
local v10 = Humanoid.Animator:LoadAnimation(Hover)
local v11 = Humanoid.Animator:LoadAnimation(Fly)
local Camera = game.Workspace.Camera
local function u2()
	if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
		return Humanoid.MoveDirection
	end
	local v12 = (Camera.CFrame * CFrame.new((CFrame.new(Camera.CFrame.p, Camera.CFrame.p + Vector3.new(Camera.CFrame.lookVector.x, 0, Camera.CFrame.lookVector.z)):VectorToObjectSpace(Humanoid.MoveDirection)))).p - Camera.CFrame.p;
	if v12 == Vector3.new() then
		return v12
	end
	return v12.unit
end
local Flymoving = script.Flymoving
local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local Flying = false

game:GetService("RunService").RenderStepped:Connect(function()
	if script.Parent == Character then
		if Flying == true then
			Humanoid:ChangeState(6)
			v3.CFrame = game.Workspace.Camera.CFrame
			if u2() == Vector3.new(0, 0, 0) then
				Flymoving.Value = false
			else
				Flymoving.Value = true
			end
			TweenService:Create(BodyVelocity, TweenInfo.new(0.3), {Velocity = u2() * 350}):Play()
		end

	end
end)

Flymoving.Changed:Connect(function(p1)
	if p1 == true then
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 100}):Play()
		v10:Stop()
		Sound1:Play()
		v11:Play()
		return
	end
	if p1 == false then
		TweenService:Create(Camera, TweenInfo.new(0.5), {FieldOfView = 70}):Play()
		v11:Stop()
		v10:Play()
		Sound1.Volume = 0.4
		wait(0.05)
		Sound1.Volume = 0.35
		wait(0.05)
		Sound1.Volume = 0.3
		wait(0.05)
		Sound1.Volume = 0.25
		wait(0.05)
		Sound1.Volume = 0.2
		wait(0.05)
		Sound1.Volume = 0.15
		wait(0.05)
		Sound1.Volume = 0.1
		wait(0.05)
		Sound1.Volume = 0
		wait(0.02)
		Sound1:Stop()
		Sound1.Volume = 0.5
	end
end)

local xractivated = false


flyEvent.OnClientEvent:Connect(function(gameProcessed)
	print("flyEvent Recived")
	if gameProcessed then return end
	if Flying == false then
		Flying = true
		if Character:FindFirstChild("HumanoidRootPart") then
			v10:Play(0.1, 1, 1)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
			Humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, false)
			Character.HumanoidRootPart.Running.Volume = 0
			Humanoid:ChangeState(6)
			BodyVelocity.Parent = Character.HumanoidRootPart
			v3.Parent = Character.HumanoidRootPart
		end
	else
		Flying = false
		Flymoving.Value = false
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, true)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, true)
		Humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, true)
		Character.HumanoidRootPart.Running.Volume = 0.65
		Humanoid:ChangeState(8)
		BodyVelocity.Parent = Character
		v3.Parent = Character
		v10:Stop()
		v11:Stop()
	end
end)

Got it!

Accodring to the Roblox create documentation

You cannot fire a remote event from a client to another client, unless you are using a third-party server script to connect the communication

Create a new script in ServerScriptService

game.ReplicatedStorage.Events.flyEvent.OnServerEvent:Connect(function(Player)
    game.ReplicatedStorage.Events.flyEvent:FireClient(Player)
end)

This does not replace any script btw!

1 Like

FINALLY, IT WORKS! Yayayay! thank you very much

1 Like