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

Player parameter happens to not work on RemoteEvent?

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.

1 answer

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

1 comment

0

The server script doesn't work. So far I have found multiple flaws in your answer. The first parameter to OnServerEvent is a Player. The parent of that player object is the Players service. Also you didn't spell GetPlayerFromCharacter right, but like I said before the parent of the Player object is the Players service. Also I don't think WaitForChild returns a bool value once called.

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