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

How do you use RemoteEvents properly?

Hey, I'm trying to make a hammer that has to be charged before being able to be swung. I'm in the knockback system part and this is what I've made with it so far...

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 canKnockback = 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		
		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)
		
		remoteEvent:FireServer("charged")
		
		charged = false
	end
end)

Server:

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

local remoteEvent = tool.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(canKnockback)
	if canKnockback == "charged" then
		head.Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			local character = humanoid.Parent
			local torso = character:WaitForChild("UpperTorso")

			if humanoid then
				torso.Transparency = 0.5
			end
		end)		
	end
end)

I'm new to RemoteEvents. Can someone explain what's going on here? Thanks much!

1 answer

Hello!

In your server script line 6, '.OnServerEvent' has 1 parameter that is passed when you do :FireServer() which is 'player'.

So it would look like this:

remoteEvent.OnServerEvent:Connect(function(player)

In your local script line 62, you are passing "charged". That means you need to add 1 more parameter in your server script

remoteEvent.OnServerEvent:Connect(function(player, canKnockback)
print(player) -- prints the player who fires it
print(canKnockback) -- prints 'charged'

Full script:

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

local remoteEvent = tool.RemoteEvent

remoteEvent.OnServerEvent:Connect(function(player, canKnockback)
    if canKnockback == "charged" then
        head.Touched:Connect(function(hit)
            local humanoid = hit.Parent:FindFirstChild("Humanoid")
            local character = humanoid.Parent
            local torso = character:WaitForChild("UpperTorso")

            if humanoid then
                torso.Transparency = 0.5
            end
        end)        
    end
end)

4 comments

0

Hello! Uhh, I did that and changed canKnockback to command so Line 06 looks like this now...

0

It still doesn't work tho.

0

The torso still becomes transparent even way before I've released.

0

Hm then there is a problem with your code

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