Hello. I currently have a GUI Text Button set up to toggle a ForceField on a player's character. Because I am using Filtering Enabled (or Experimental Mode is off), i must use a RemoteEvent. I have a LocalScript set up to get input from the button and change a few GUI elements. The LocalScript is only capable of changing the GUI elements and not the player's ForceField. I use the FireServer()
function to call a listener in a Server Script. On the Server Script, I have a variable defining the player's character. The first default parameter is the player associated with the client that sent the function. All player instances have a Character property, but for some reason it doesn't work. The code in the Server and Local Scripts are below. I also attached an image link to the setup I have.
Setup Image Link
-- Server Script local event = script.Parent:WaitForChild("RemoteEvent") event.OnServerEvent:Connect(function(player) local char = player.Character local forcefield = char:FindFirstChild("ForceField") if forcefield then forcefield:Destroy() else c = Instance.new("ForceField") c.Parent = char end end)
-- Local Script local event = script.Parent:WaitForChild("RemoteEvent") local button = script.Parent local bg = script.Parent.Parent local function forcefieldEvent() local player = game.Players.LocalPlayer local char = player.Character local foundFF = char:FindFirstChild("ForceField") if foundFF then button.Text = "PVP Mode" bg.Style = Enum.FrameStyle.ChatRed else button.Text = "Safe Mode" bg.Style = Enum.FrameStyle.ChatGreen end event:FireServer() end button.MouseButton1Click:Connect(forcefieldEvent)
I have tried adding another parameter that points to the Local Player, but that didn't work. Please help, thank you to whoever responds.
I did some minor changes to the server script
-- Server Script local event = script.Parent:WaitForChild("RemoteEvent") event.OnServerEvent:Connect(function(hit) -- Changed Parameter to hit local player = game.Players:GetPlayersFromCharacter(hit.Parent) -- Added a variable that gets the local player local char = player.Character local forcefield = char:WaitForChild("ForceField") -- Changed FindFirstChild to WaitForChild if forcefield then forcefield:Destroy() else c = Instance.new("ForceField") c.Parent = char end end)
Hopefully this helped you fix your server script