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

how to fix OnServerEvent can only be used on the server?

here the script

math.randomseed(tick())

  

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local punchEvent = Instance.new("RemoteEvent", ReplicatedStorage)

punchEvent.Name = "PunchEvent"

  

local animations = {2729957815, 2733095250, 2734153485}

  

local function onPunchFired(plr)

local char = game.Workspace:FindFirstChild(plr.Name)

local humanoid = char.Humanoid

local animation = Instance.new("Animation")

local picked = math.random(1, #animations)

animation.AnimationId = "http://roblox.com/asset/?id="..animations[picked]

local animTrack = humanoid:LoadAnimation(animation)

animTrack:Play()

local dmgScript = script.DmgScript:Clone()

if picked == 1 then

dmgScript.Parent = char.RightHand

elseif picked == 2 then

dmgScript.Parent = char.LeftHand

elseif picked == 3 then

dmgScript.Parent = char.RightFoot

end

dmgScript.Disabled = false

wait(.4)

dmgScript:Destroy()

print("Damage was did done did")

end

  

punchEvent.OnServerEvent:Connect(onPunchFired)

here the problem

22:44:00.497 - Touched is not a valid member of Script

22:44:00.498 - Stack Begin

22:44:00.498 - Script 'ServerScriptService.Punch.DmgScript', Line 1

22:44:00.499 - Stack End

22:44:02.262 - OnServerEvent can only be used on the server -- this

22:44:02.262 - Stack Begin

22:44:02.263 - Script 'Workspace.lord_lol.Punch', Line 31

22:44:02.263 - Stack End

22:44:05.513 - Infinite yield possible on 'ReplicatedStorage:WaitForChild("PunchEvent")'

22:44:05.514 - Stack Begin

22:44:05.514 - Script 'ServerScriptService.Punch', Line 3

22:44:05.515 - Stack End

2 comments

0

u need to create the remote event outside the script and then do game.ReplicatedStorage:WaitForChild("remotename").OnServerEvent:Connect(function() and function after that.

0

where do i create remote event?

1 answer

✓ Accepted answer

The event OnServerEvent is an event of RemoteEvent that fires when a function is called from a LocalScript, can be called/fired with :FireServer while the eventOnClientEvent fires when a function is called from a Server-Side Script, can be either :FireClient or :FireAllClient. See here for more details. For future confusions, I think 'On' will be of help.

LocalScript (Client-Side) then 'On'ClientEvent OnClientEvent :FireServer() Script (Server-Side) then 'On'ServerEvent OnServerEvent :FireClientor :FireAllClient()

So for line #61, it should be RemoteEvent.OnClientEvent:Connect(onPunchFired)

Hopefully, this answers your problem.

1 comment

0

I am also recommending you this video: https://www.youtube.com/watch?v=4Dc_bri9mjs : It will explain everything.

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