Passing Variables through remote functions

Firstly, I want to know if it is possible to even pass variables through remote functions. I am trying to do a complete combat, and I am having trouble detecting combos on server script when I have it on local script. Basiclly depending on the combo, I want it to do more damage.

Local script:



local ServerEvent = game.ReplicatedStorage:WaitForChild("ServerEvent")
local UserInputService = game:GetService("UserInputService")
local tool = script.Parent

local idleAnim = tool:WaitForChild("CombatIdle")
local blockAnim = tool:WaitForChild("Block")
local combo1 = tool:WaitForChild("Combo1")
local combo2 = tool:WaitForChild("Combo2")
local combo3 = tool:WaitForChild("Combo3")

local equipped = false
local canAttack = true

local combo = 0
local timeUntilComboResets = 1.5
local lastTimeClicked = tick()

BlockKey = Enum.KeyCode.R

tool.Equipped:Connect(function()
	equipped = true
	character = tool.Parent
	isPlayerBlocking = character:FindFirstChild("isPlayerBlocking")
	humanoid = character:FindFirstChild("Humanoid")
end)

tool.Activated:Connect(function()
	if equipped and isPlayerBlocking.Value == false and canAttack == true then
		if tick() - lastTimeClicked >= timeUntilComboResets then
			combo = 0
		end
		lastTimeClicked = tick()
		combo = combo + 1
		print(combo)
		if combo == 1 then
			combo1Track = humanoid:LoadAnimation(combo1)
			combo1Track:Play()
			ServerEvent:FireServer(combo)
			canAttack = false
			wait(0.3)
			canAttack = true
		elseif combo == 2 then
			combo2Track = humanoid:LoadAnimation(combo2)
			combo2Track:Play()
			ServerEvent:FireServer(combo)
			canAttack = false
			wait(0.3)
			canAttack = true
		elseif combo == 3 then
			combo1Track:Play()
			ServerEvent:FireServer(combo)
			canAttack = false
			wait(0.3)
			canAttack = true
		elseif combo >= 4 then
			combo3Track = humanoid:LoadAnimation(combo3)
			combo3Track:Play()
			ServerEvent:FireServer(combo)
			combo = 0
			canAttack = false
			wait(0.3)
			canAttack = true
		end		
	end
end)

tool.Unequipped:Connect(function()
	combo = 0
	equipped = false
	character = nil
	isPlayerBlocking = nil
	humanoid = nil
end)


UserInputService.InputBegan:Connect(function(InputObject,gameProcessed)
	if InputObject.KeyCode == BlockKey and isPlayerBlocking.Value == false and equipped == true then
		isPlayerBlocking.Value = true
		print("BLOCKING")
		BlockTrack = humanoid:LoadAnimation(blockAnim)
		BlockTrack:Play()
		BlockTrack.Looped = true
	end
end)

UserInputService.InputEnded:Connect(function(InputObject,gameProcessed)
	if InputObject.KeyCode == BlockKey and isPlayerBlocking.Value == true and equipped == true then
		isPlayerBlocking.Value = false
		print("BLOCK ENDED!")
		BlockTrack:Stop()
		BlockTrack.Looped = false
	end
end)

Server script:



local ServerEvent = game.ReplicatedStorage:WaitForChild("ServerEvent")
local tool = script.Parent
local hitAnim = tool:WaitForChild("GettingHit")

local equipped = false

tool.Equipped:Connect(function()
	Ourcharacter = tool.Parent
	equipped = true
end)

local humanoidsHit = {}

local damage = 5
local hitWalkSpeed = 2

ServerEvent.OnServerEvent:Connect(function(combo)
	for i,v in pairs(game.Workspace:GetChildren("Model")) do
		if v:IsA("Model") then
			if v.Humanoid and v.HumanoidRootPart then
				if v ~= Ourcharacter then
					local otherRootPart = v.HumanoidRootPart
					local ourRootPart = Ourcharacter.HumanoidRootPart
					local magnitude = (ourRootPart.Position - otherRootPart.Position).Magnitude
					local unit = (ourRootPart.Position - otherRootPart.Position).Unit
					local dot = unit:Dot(Ourcharacter.Head.CFrame.LookVector)
					if magnitude <= 2 and dot <= 0.5 then
						local descandants = v:GetDescendants()
						for i, descandant in pairs(descandants) do
							if descandant:IsA("BoolValue") then
								local isPlayerBlocking = descandant
								if isPlayerBlocking.Name == "isPlayerBlocking" then
									if isPlayerBlocking.Value == true then
										print("OTHER PLAYER IS BLOCKING")
									else
										print("PLAYER IS NOT BLOCKING")
										if combo >= 4 then
											print("Combo 4")
										end
										local hitTrack = ` 
    v.Humanoid:LoadAnimation(hitAnim)
										hitTrack:Play()
										v.Humanoid.WalkSpeed = 2
									end
								end
							end
						end
					end
				end
			end
		end
	end
end)



The first parameter is always the player who fired, by default. Try that instead:

ServerEvent.OnServerEvent:Connect(function(player, combo)

Hmmm I didn’t think that would work but it did. Can you explain it more in depth on how u solved this I really need to understand how this works. Thank you for the help.

Both remote functions and events (when received on the server) have the player who fired the remote as the first parameter.
Roblox does it by default.

Here is an example:

-- client

remote:FireServer()  -- no arguments
-- server

remote.OnServerEvent:Connect(function(PlayerWhoFired)
   print(PlayerWhoFired.Name .. ' fired the event!')
end)

If you want to send arguments, it would be like this:

-- client

local animal = 'Dog'
remote:FireServer(animal)
-- server

remote.OnServerEvent:Connect(function(player, animal)
   print(player.Name .. ' sent the animal: ' .. animal)
end)

You can also send multiple arguments, example:

-- client

remote:FireServer(1, 2, 3, 4, 5)
-- server

remote.OnServerEvent:Connect(function(player, one, two three, four, five)
   print(one, two, three, four, five)
end)

-- or

remote.OnServerEvent:Connect(function(player, ...)
   print(...)
end)

If you want to read more:

5 Likes

Thank you for the explanation, it is really detailed.

2 Likes