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

How do I fire an event to the client using a server script?

This script is supposed to get the player inside and deal damage to them, but also makes them flinch. I'm trying to use a RemoteEvent to tell the player that's being damaged to start the flinch animation.

My script errors and says: Unable to cast value to object I am also using a pcall in case it does error which it did.

Server Script

local replicatedStorage = game:GetService("ReplicatedStorage")
local combatEvent = replicatedStorage:WaitForChild("combat")

local dmg = 5
local hitboxSize = 2

combatEvent.OnServerEvent:Connect(function(player, action, V1)
	local character = player.Character
	
	if action == "combat" then
		local Region = Region3.new(V1 - Vector3.new(hitboxSize,hitboxSize,hitboxSize),V1 + Vector3.new(hitboxSize,hitboxSize,hitboxSize))
		local RTable = workspace:FindPartsInRegion3(Region, nil, 100)
		
		local prt = Instance.new("Part", game.Workspace)
		prt.Anchored = true
		prt.CanCollide = false
		prt.CFrame = Region.CFrame
		prt.Size = Region.Size
		game.Debris:AddItem(prt,1)
		
		local Deb = "Deb"
		local Humanoid = "Humanoid"
		print("Running")
		for i,v in pairs(RTable) do
			if v.Parent:findFirstChild(Humanoid) and v.Parent:findFirstChild(Deb) == nil and v.Parent ~= character then
				print("Hit")
				local Deb = Instance.new("BoolValue", v.Parent)
				Deb.Name = tostring(Deb)
				game.Debris:AddItem(Deb,.2)
				
				local enemyName = v.Parent.Name
				local enemyCombat = v.Parent:GetChildren()
				
				print(enemyName)
				
				local success, response = pcall(combatEvent.FireClient, combatEvent, "Player1")
				print(success, " - Error: ",response)
				
				v.Parent.Humanoid:TakeDamage(dmg)
				
				break
			end
		end
	end
end)

Local Script

local replicatedStorage = game:GetService("ReplicatedStorage")

local combatEvent = replicatedStorage:WaitForChild("combat")

local canAttack = true

local function flinch()
	canAttack = false
	
	print("I should be flinching ")
	canAttack = true
end

combatEvent.OnClientEvent:Connect(flinch)

1 comment

0

You might be asleep right now, but could you tell me which line the error occurs on?

1 answer

You should use :FireClient() to fire a event to a client or :FireServer() to fire a event to the whole server.

I'll make a little example of how to use it(with :FireClient())

-- server script

local event -- type here the location of the RemoteEvent

local ClickDetector = workspace.Part.ClickDetector

ClickDetector.MouseClick:Connect(function(plr)
event:FireClient(plr) -- you will always need the client as the first parameter when firing a event from the server because the event needs to know to what player it should fire the event(you will not need to do this with :FireServer())
end)

-- local script

local event 
local plr = game:GetService('Players').LocalPlayer

event.OnClientEvent:Connect(function() -- you will not need the player parameter here because you can get the player with LocalPlayer
print('hello from the server to the client '.. plr.Name..’!')
end)

4 comments

0

Where you write plr in FireClient() you have to have the player name or the actual player under Players in the Explorer?

0

I still get the same error

0

you need to write whatever is best for you in the script, and also you would need the player instance inside the :FireClient() parameters

0

and I actually made a little mistake, I forgot to add “..” to plr.Name, I fixed it.

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