Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

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)
0
You might be asleep right now, but could you tell me which line the error occurs on? Neatwyy 123 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
Where you write plr in FireClient() you have to have the player name or the actual player under Players in the Explorer? RichCookieGamer 7 — 3y
0
I still get the same error RichCookieGamer 7 — 3y
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 Sarturex857 109 — 3y
0
and I actually made a little mistake, I forgot to add “..” to plr.Name, I fixed it. Sarturex857 109 — 3y
Ad

Answer this question