Create and destroy forcefield in character with RemoteEvents?

I’m trying to create a forcefield to make the player’s character indestructible while inside a menu; when they close the menu, I’m trying to find and destroy a forcefield in the character.

The issue is that I cannot use LocalCharacter from a server script, so I must use a RemoteEvent to obtain information from the client.

I’ve tried a few things but nothing seems to be working; any advice?

This is my client script:

local char = game:GetService("Players").LocalPlayer.Character
script.Parent.FF:FireServer(char)

And this is my server script:

local ff = Instance.new("ForceField")

local remoteEvent = script.Parent:WaitForChild("FF")

local function onCreatePart(char)
	ff.Parent = char
end


local remoteEventD = script.Parent:WaitForChild("FFD")

local function onDestroyPart(char)
	local descendants = char:GetDescendants()
	for index, descendant in pairs(descendants) do
		if descendant:IsA("ForceField") then
			descendant:Destroy()
		end
	end
end

remoteEvent.OnServerEvent:Connect(onCreatePart)

remoteEventD.OnServerEvent:Connect(onDestroyPart)
4 Likes

If I remember correctly the OnServerEvent event takes a player parameter as the first one which defaults to the player given by the LocalScript where the event was fired, which means the char parameter you are passing defaults to the default player parameter, try replacing your char parameter with a “plr” one and then add the char parameter as an additional one.

local function onDestroyPart(plr, char)

If this is not your issue please elaborate further on what’s not working.

2 Likes
local ff = Instance.new("ForceField")

local remoteEvent = script.Parent:WaitForChild("FF")

local function onCreatePart(char)
	ff.Parent = char
end


local remoteEventD = script.Parent:WaitForChild("FFD")

local function onDestroyPart(player)
	local descendants = player.Character:GetDescendants()
	for index, descendant in pairs(descendants) do
		if descendant:IsA("ForceField") then
			descendant:Destroy()
		end
	end
end

remoteEvent.OnServerEvent:Connect(onCreatePart)

remoteEventD.OnServerEvent:Connect(onDestroyPart)
1 Like

change the server script to this:

local ff = Instance.new("ForceField")

local remoteEvent = script.Parent:WaitForChild("FF")

local function onCreatePart(plr,char)
	ff.Parent = char
end


local remoteEventD = script.Parent:WaitForChild("FFD")

local function onDestroyPart(plr,char)
	local descendants = char:GetDescendants()
	for index, descendant in pairs(descendants) do
		if descendant:IsA("ForceField") then
			descendant:Destroy()
		end
	end
end

remoteEvent.OnServerEvent:Connect(onCreatePart)

remoteEventD.OnServerEvent:Connect(onDestroyPart)
1 Like

I know you’re just copying the code provided but to find the forcefield you could just do the following.

local forcefield = char:FindFirstChildOfClass("ForceField")
if forcefield then
1 Like