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

Why is this Remote Event having difficulty working?

This script is the server script connected to a running event, when the event runs it attached a model to the character. I know for certain the remote event is running fine and the script in question works outside an event without difficulty. Is there any specific reason however why it won't work inside a Remote Event script?

local event2 = game:GetService("ReplicatedStorage"):WaitForChild("EarsBEvent")
event2.OnServerEvent:Connect(function(player)
	if player:findFirstChild("Humanoid") ~= nil then
		local g = script.Parent.EarsBack:Clone()
		g.Parent = player.Parent
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].className == "Part" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = player.Parent.Torso
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end
end)

edit: The problem is definitely from line 3/4 onward, script cannot find 'EarsBack' model

1 answer

✓ Accepted answer

Your problem is that when the server receives player, it's not your character, but rather the child of Players. Fix this by changing it to player.Character

local event2 = game:GetService("ReplicatedStorage"):WaitForChild("EarsBEvent")
event2.OnServerEvent:Connect(function(player)
    if player.Character:FindFirstChild("Humanoid") ~= nil then
        local g = script.Parent.EarsBack:Clone()
        g.Parent = player.Parent
        local C = g:GetChildren()
        for i=1, #C do
            if C[i].className == "Part" then
                local W = Instance.new("Weld")
                W.Part0 = g.Middle
                W.Part1 = C[i]
                local CJ = CFrame.new(g.Middle.Position)
                local C0 = g.Middle.CFrame:inverse()*CJ
                local C1 = C[i].CFrame:inverse()*CJ
                W.C0 = C0
                W.C1 = C1
                W.Parent = g.Middle
            end
                local Y = Instance.new("Weld")
                Y.Part0 = player.Parent.Torso
                Y.Part1 = g.Middle
                Y.C0 = CFrame.new(0, 0, 0)
                Y.Parent = Y.Part0
        end

        local h = g:GetChildren()
        for i = 1, # h do
            h[i].Anchored = false
            h[i].CanCollide = false
        end

    end
end)

Hope this helped!

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