Remote Event Question

  1. I want to know If the script I wrote out would work or makes sense

  2. How do I make a different script accept this remote event signal or whatever its called.

My intention for this script is when the tool “punch” is activated it tells the server script to play the punch animation and if the punch hits something for the server to deal damage to the person your hitting. I’ll make a different script that gives damage and plays the animation.

If this script doesn’t make any sense or completely wrong then please don’t hate on me because this is my first time using remote events.

The 2 remote events anim and punch are in server storage, cause why not. Also i was told that animations and damage must be played on the server side.

Anyways here is the script:

local fist = script.Parent.Parent
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")


local server = game.ServerStorage
local Hit = server.Punch
local anim = server.Anim

function onActivation1()
	Hit.OnServerEvent:Connect(onActivation)
	anim:FireServer()
end


function onActivation()




	fist.Handle.Touched:connect(function(hit) 
		
		if hit.Parent:FindFirstChild('Humanoid') then
			local humanoid1 = hit.Parent:FindFirstChild('Humanoid') 
			local PlayerWhoGotHit = game.Players:GetPlayerFromCharacter(hit.Parent)

		Hit:FireServer()
		

		
		end

	end)
	end

fist.Activated:Connect(onActivation1)

Thanks

Local scripts can’t access server storage. Try putting the remote events in replicated storage instead. You could also detect the fist collisions on the server to avoid using remote events.

Ok Ill move the events to replicated storage. But would the script work? And also how would I make another script accept the remote event signals?

I also just wanna keep using remote events for the practice.

The script should still work. After you use :FireServer(), a server script is able to use RemoteEvent.OnServerEvent. You can use it by doing something like this:

--Local Script
RemoteEvent:FireServer("Hello")
wait(1)
RemoteEvent:FireServer("This is a test")

--Server Script
RemoteEvent.OnServerEvent:Connect(function(Message)
    print(Message)
end)

--Output
Server - "Hello"
Server - "This is a test"

A small side note, but OnServerEvent has 1 parameter before actually referencing your other custom parameters which is the Player Instance who fired the event

--Local Script
RemoteEvent:FireServer("Hello")
wait(1)
RemoteEvent:FireServer("This is a test")

--Server Script
RemoteEvent.OnServerEvent:Connect(function(Player, Message)
    print(Message)
end)

--Output
Server - "Hello"
Server - "This is a test"

I would disagree with this, as it can be laggy for players. if the player has a high latency, then the hit detection would be useless, as the player has probably already moved out of the way by the time the server receives the event.

Its better practice to replicate hit detection from the client, and replicate effects to each client, rather than letting the server handle it

local handle = script.Parent
local fist = handle.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local storage = game:GetService("ReplicatedStorage")
local punch = storage:WaitForChild("Punch")
local anim = storage:WaitForChild("Anim")

fist.Activated:Connect(function()
	handle.Touched:Connect(function(hit)
		local hitPlayer = game.Players:GetPlayerFromCharacter(hit.Parent)
		if hitPlayer then
			punch:FireServer()
		end
	end)
end)

ServerStorage can’t be accessed from local scripts (so move everything to ReplicatedStorage instead), anyway here is what I believe you intended to do, can I see the corresponding server script which is being fired?

Woops, nice catch. I hope I didn’t confuse you @trueblockhead101.

Hit detections are fired by the client. If you’re lagging you’ll still be able to fire the touched event if the part has your network ownership. Remote events should be avoided unless necessary to avoid exploits anyways.