How do you pass a value from client to server by RemoteEvent?

I’m trying to make a hammer that has to be charged before being able to be swung. And by swinging, it’ll do knockback to hit. But the problem is, it still does knockback even without swinging. (Just by touching the dummies)

This is what the scripts looks like right now…
Client:

local tool = script.Parent

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local rootPart = character:WaitForChild("HumanoidRootPart")

local remoteEvent = tool.RemoteEvent

local charge = script.Charge
local smash = script.Smash

local chargeTrack = animator:LoadAnimation(charge)
local smashTrack = animator:LoadAnimation(smash)

local cooldown = false
local charged = false
local hanging = false
local canKnockback = false

tool.Activated:Connect(function()
	if not cooldown and not hanging then
		cooldown = true
		
		if not charged then
			smashTrack:Stop()
			chargeTrack:Play()
			
			delay(chargeTrack.Length - 0.02999, function()
				chargeTrack:AdjustSpeed(0)
			end)
			
			charged = true
		end
		
		wait(1)
		cooldown = false
	end
end)

tool.Deactivated:Connect(function()
	chargeTrack:Stop()
	
	if charged then
		remoteEvent:FireServer(charged)
		
		smashTrack:Play()
		
		delay(smashTrack.Length - 0.02999, function()
			smashTrack:AdjustSpeed(0)
			
			rootPart.Anchored = true
			hanging = true
			
			wait(1)
			smashTrack:Stop()
			
			rootPart.Anchored = false
			hanging = false
		end)
	end
	
	charged = false
end)

Server:

local tool = script.Parent
local head = tool.Handle

local remoteEvent = tool.RemoteEvent

local function knockback()

end

remoteEvent.OnServerEvent:Connect(function(player, command)
	if command then
		head.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Knockback")  then
				if not script.Parent:FindFirstChild(hit.Parent.Name) then

					local debris = game:GetService("Debris")

					local tag = Instance.new("IntValue")
					tag.Parent = script.Parent
					tag.Name = hit.Parent.Name
					debris:AddItem(tag, 1)

					local Intensity = 1000

					local Knockback = Instance.new("BodyVelocity")
					Knockback.Parent = hit.Parent.HumanoidRootPart
					Knockback.Velocity = head.CFrame.LookVector * Intensity 
					Knockback.MaxForce = Knockback.MaxForce * Intensity 

					debris:AddItem(Knockback, 0.2)

				end
			end
		end)
	end	
end)

If you can help, PLEASE do. I badly need help. I’ve been trying and trying for so long and it’s becoming painfully stressful. Thanks! <3

1 Like

All you’d just really need to do is pass the tuple argument in this parameter I believe, but which one would you really want though from your Bools? :sweat_smile: I’m gonna guess the charged one

remoteEvent:FireServer(charged) 

oh ive typed it wrong again yes

remoteEvent:FireServer(charged)

is what is in there but same issue. it still does knockback even way after swinging

Ive edited it now. PLEASE help if you still can.

if charged then
	remoteEvent:FireServer()

If the thing you want to pass through here is “command”, then do

		remoteEvent:FireServer(command) 

If there’s anything you want to send through a remote event, just put that thing in the parentheses.

it’s the charged it self. i edited the script above. so same issue, it still does knockback even way after swinging. by touching

I’ll try to investigate further into your problem. Hopefully you’ll be able to figure out what’s causing this.

Could you do
if command == true then
To see if there’s any difference?

thank you, please do. i am constantly trying things out but nothing works and its stressing me out

i remember trying this and theres still no difference. :frowning:

Strange, could you try printing out the command variable to see what you get?

like this?

remoteEvent.OnServerEvent:Connect(function(player, command)
	print(command)
	
	if command == true then
		head.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Knockback")  then
				if not script.Parent:FindFirstChild(hit.Parent.Name) then

					local debris = game:GetService("Debris")

					local tag = Instance.new("IntValue")
					tag.Parent = script.Parent
					tag.Name = hit.Parent.Name
					debris:AddItem(tag, 1)

					local Intensity = 1000

					local Knockback = Instance.new("BodyVelocity")
					Knockback.Parent = hit.Parent.HumanoidRootPart
					Knockback.Velocity = head.CFrame.LookVector * Intensity 
					Knockback.MaxForce = Knockback.MaxForce * Intensity 

					debris:AddItem(Knockback, 0.2)

				end
			end
		end)
	end	
end)

Yeah, see what kind of Output you get from that

it prints true everytime i deactivate.

I would recommend not creating the connection to the .Touched event within the RemoteEvent. Create it outside of the RemoteEvent, and have the RemoteEvent set a variable to true. Then from within the .Touched event check to see if that variable is set to true, if it is, then apply knockback and set it back to false. Let me know if this fixes it for you.

u mean like this?

local tool = script.Parent
local head = tool.Handle

local remoteEvent = tool.RemoteEvent

local function knockback()
	head.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Knockback")  then
			if not script.Parent:FindFirstChild(hit.Parent.Name) then

				local debris = game:GetService("Debris")

				local tag = Instance.new("IntValue")
				tag.Parent = script.Parent
				tag.Name = hit.Parent.Name
				debris:AddItem(tag, 1)

				local Intensity = 1000

				local Knockback = Instance.new("BodyVelocity")
				Knockback.Parent = hit.Parent.HumanoidRootPart
				Knockback.Velocity = head.CFrame.LookVector * Intensity 
				Knockback.MaxForce = Knockback.MaxForce * Intensity 

				debris:AddItem(Knockback, 0.2)

			end
		end
	end)
end

remoteEvent.OnServerEvent:Connect(function(player, command)
	print(command)
	
	if command then
		knockback()
	end	
end)

Something more like this:

local tool = script.Parent
local head = tool.Handle

local remoteEvent = tool.RemoteEvent
local knockback = false

head.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("Knockback")  then
		if not script.Parent:FindFirstChild(hit.Parent.Name) then
			
			if knockback == true then

				local debris = game:GetService("Debris")

				local tag = Instance.new("IntValue")
				tag.Parent = script.Parent
				tag.Name = hit.Parent.Name
				debris:AddItem(tag, 1)

				local Intensity = 1000

				local Knockback = Instance.new("BodyVelocity")
				Knockback.Parent = hit.Parent.HumanoidRootPart
				Knockback.Velocity = head.CFrame.LookVector * Intensity 
				Knockback.MaxForce = Knockback.MaxForce * Intensity 

				debris:AddItem(Knockback, 0.2)
				
				knockback = false
			end

		end
	end
end)

remoteEvent.OnServerEvent:Connect(function(player, command)
	print(command)

	if command then
		knockback = true
	end	
end)
1 Like

You can also disconnect the connection. I’m on mobile so exercise the formatting.

connection = head.Touched:Connect(function()
—knockbacknhere

     Connection:disconnect()

end)

pls elaborate i dont really get it sorry

I got so much closer to what I want now, thanks to you!!! But there’s still a problem…

So, when I swing the hammer in air, (No dummies were hit) and then I go and touch a dummy without swinging, it does knockback. Please help if you still can, thanks again!