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

OnServerEvent can only be used on the server error? [FIXED]

(r6)

So I was making my script for a sword, but the M2 script won't work. it says that OnServerEvent can only be used on the server. (There is a Remote Event that the M2 references.)

local m2 = game.ReplicatedStorage.Combat:WaitForChild("Punch2Player")
local tweenService = game:GetService("TweenService")

m2.OnServerEvent:Connect(function(player)
	local character  = player.Character


	local Track5 = Instance.new("Animation")
	Track5.AnimationId = "rbxassetid://5740595758"
	local Anim5 = character.Humanoid:LoadAnimation(Track5)
	Anim5:Play()




	for i,v in pairs(workspace:GetChildren()) do
		if   v:FindFirstChild("HumanoidRootPart") and v:FindFirstChild("Humanoid") and v ~= character then
			if (v.HumanoidRootPart.Position - character.HumanoidRootPart.Position).Magnitude < 4 then

				if v.HumanoidRootPart:FindFirstChild("Block") then
					v.BlockBar.Value = 0

				else

					local hiteffect = game.ReplicatedStorage.Combat:WaitForChild("SlashEffect"):Clone()

					hiteffect.CFrame = v.HumanoidRootPart.CFrame * CFrame.Angles(0,math.rad(177.5),math.rad(-90))
					hiteffect.BrickColor = BrickColor.new("Persimmon")
					hiteffect.Parent = workspace
					game.Debris:AddItem(hiteffect,0.5)

					local info3 = TweenInfo.new(0.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0)
					local goal3 = {}
					goal3.Size = hiteffect.Size * 3
					goal3.Transparency = 1
					local tween3 = tweenService:Create(hiteffect,info3,goal3)
					tween3:Play()
					print("played3")
					print("you both hit at the same time")


					v.Humanoid:TakeDamage(15)	
					local bv = Instance.new("BodyVelocity",v.HumanoidRootPart)
					bv.MaxForce = Vector3.new(1e8,1e8,1e8)
					bv.Velocity = character.HumanoidRootPart.CFrame.lookVector*80
					game.Debris:AddItem(bv,0.3) 

				end

			end
		end
	end

end)

The error is apparently at line 4, And I can't see the error in that line, anybody help?

4 answers

You probably put this in a localscript, you need to use onserverevent in a normal server script.

2 comments

0

The script normally has every requirement in it including animations and the other script, where should I put it?

0

Don't worry, the error has been fixed!

All remote events scripts should be in serverscriptstorage (not the remotes).

It may be that you never put an end to the OnServerEvent:

m2.OnServerEvent:Connect(function(player)
	local character  = player.Character
	-- You didn't end this function

Thats because you need to use OnServerEvent inside server script (normal script) also other players can see Animations from LocalScript, its ok

inside that local script type

game.ReplicatedStorage.Combat:WaitForChild("Punch2Player"):FireServer() -- if Punch2Player is RemoteEvent

and then put server script inside that RemoteEvent or where you want

script.Parent.OnServerEvent:Connect(function(player) -- if somebody fires that event, if you dont have that script inside RemoteEvent then you know what to do right :p

and there is rest of your code

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