Forcefield Not Working

Hello
“Forcefield” is activated, but only for a character that activates the script (It is displayed)
For other players but “Forcefield” did not exist and the character is not protected

local Players = game:GetService("Players")
local hlavak = script.Parent
local ATMs = game.Workspace:FindFirstChild("ATMs")

local function activateForceFieldOnPlayer(player)  ---- Aktivuje FORCEFIEL
    local character = player.Character
    if character then
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            local forceField = Instance.new("ForceField")
			forceField.Visible = true
            forceField.Parent = character
        end
    end
end


local function disablePlayerMovement(player)    ---- Deaktivuje POHYB hráče 
    local character = player.Character
    if character then
        local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        if humanoidRootPart then
            humanoidRootPart.Anchored = true -- Uzamkne hráče na místě
        end
        local controls = require(player.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
        controls:Disable() -- Vypne kontrolu pohybu hráče
    else
        player.CharacterAdded:Wait() -- Počkáme, až bude hráč plně načtený do hry
        disablePlayerMovement(player) -- Rekurzivně zavolám disablePlayerMovement pro hráče, který byl přidán
    end
end



if ATMs then
    for _, ATM in ipairs(ATMs:GetChildren()) do
        if ATM.Name == "ATM" then
            local proximityPrompt = ATM:FindFirstChild("ProximityPrompt")
            if proximityPrompt then
                proximityPrompt.Triggered:Connect(function()
                    hlavak.Frame.Visible = true
                    hlavak.BG.Visible = true
                    proximityPrompt.Enabled = false
					local player = Players.LocalPlayer
    					if player then
						disablePlayerMovement(player) -- Zamezíme hráči pohyb
        				activateForceFieldOnPlayer(player)        				
    				end
                end)
            end
        end
    end
else
    warn("ATMs not found in Workspace!")
end

thanks in advance
codycheck

1 Like

Because this code is running in a client-sided script, the Forcefield is only being created locally for that specific player. In order for the creation of the Forcefield to be replicated / seen by other players (and for the Forcefield to protect them from damage), the Forcefield would need to be created on the server side.

One way you could achieve that would be to communicate with the server via a RemoteEvent when the player activates the ProximityPrompt (however, if not implemented correctly, this could be abused by exploiters who call the RemoteEvent without ever using the ProximityPrompt).


Alternatively, the code could be split up into:

  1. A server-sided script which would handle the actions that need to be replicated (which in this case, mostly revolves around the Forcefield)

  2. And a client-sided script that focuses on the client-specific actions that don’t need to be replicated to other players in the game (such as updating the Visible property of GUI Objects and locally disabling the ProximityPrompt).


Here’s an example revision based on the code included in the original post:

Example Revision

Server Script Code

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--[[ Create a RemoteEvent in the ReplicatedStorage;
replace "NameOfRemoteEventHere" with the name you choose for the RemoteEvent --]]
local proximityPromptRemoteEvent = ReplicatedStorage.NameOfRemoteEventHere


local ATMs = workspace:FindFirstChild("ATMs")

local function activateForceFieldOnPlayer(player)  ---- Aktivuje FORCEFIEL
    local character = player.Character or player.CharacterAdded:Wait()
    local forcefieldCheck = character:FindFirstChildOfClass("ForceField")

    if character and not forcefieldCheck then -- Makes sure the Character doesn't already have a forcefield before creating a new one
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then

--[[ Consider anchoring the HumanoidRootPart here, on the server-side,
instead of via a LocalScript. I've kept that in the LocalScript for now
so you can decide if you want to make that change --]]

            local forceField = Instance.new("ForceField")
			forceField.Visible = true
            forceField.Parent = character
        end
    end
end


local function proximityPromptFunctionality(item)
    if item.Name ~= "ATM" then return end

    local proximityPrompt = item:FindFirstChildOfClass("ProximityPrompt")
    if proximityPrompt then

        proximityPrompt.Triggered:Connect(function(player)
            proximityPromptRemoteEvent:FireClient(player, proximityPrompt)
            activateForceFieldOnPlayer(player)        				
        end)

    end
end


if ATMs then

    ATMs.ChildAdded:Connect(proximityPromptFunctionality)

    for _, ATM in ATMs:GetChildren() do
        proximityPromptFunctionality(ATM)
    end

else
    warn("ATMs not found in Workspace!")
end

LocalScript Code

local Players = game:GetService("Players")
local player = players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
--[[ Create a RemoteEvent in the ReplicatedStorage;
replace "NameOfRemoteEventHere" with the name you choose for the RemoteEvent --]]
local proximityPromptRemoteEvent = ReplicatedStorage.NameOfRemoteEventHere


local hlavak = -- Reference to the ScreenGUI(?)
local Frame = hlavak:WaitForChild("Frame")
local BG = hlavak:WaitForChild("BG")

local function disablePlayerMovement()    ---- Deaktivuje POHYB hráče 
    local character = player.Character or player.CharacterAdded:Wait() -- References Character or waits for it to load in if it doesn't exist yet
    if character then
        local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        if humanoidRootPart then
            humanoidRootPart.Anchored = true -- Uzamkne hráče na místě
        end

        local controls = require(player.PlayerScripts:WaitForChild("PlayerModule")):GetControls()
        controls:Disable() -- Vypne kontrolu pohybu hráče
    end
end

proximityPromptRemoteEvent.OnClientEvent:Connect(function(proximityPrompt)
    Frame.Visible = true
    BG.Visible = true
    proximityPrompt.Enabled = false

    disablePlayerMovement() -- Zamezíme hráči pohyb
end)
2 Likes

Hello,

why is this needed (why disable the player) for a forcefield?

            humanoidRootPart.Anchored = true 

Also is there a way of having a forcefield that protects a player, but is not visible?

Thanks

That’s a gameplay decision from the developer who posted this thread (as it was included in the code block from the original post), so I kept it when creating a revised version of the code.

For reference, I’ve added [comments with brackets around it] within the quoted sections from the original code block to highlight the function that updates the Anchored property of the Character’s HumanoidRootPart as well as the place in the script where the function was called from:


Yup! When creating the Forcefield and parenting it to the Character model, you can set its Visible property to false and it should still protect the player without it being visible.

1 Like

Thanks for the help, it helped me, STRONGBIGMAN9
Thanks for help, it helped me
I had to edit the serverScript to deactivate Forcefield

print("ATMServer")

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenBankomat = ReplicatedStorage.RemoteEvents.OpenBankomat
local CloseBankomat = ReplicatedStorage.RemoteEvents.CloseBankomat
local ATMs = workspace:FindFirstChild("ATMs")

local function activateForceFieldOnPlayer(player)
    local character = player.Character or player.CharacterAdded:Wait()
    local forcefieldCheck = character:FindFirstChildOfClass("ForceField")
    if character and not forcefieldCheck then -- Makes sure the Character doesn't already have a forcefield before creating a new one
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            local forceField = Instance.new("ForceField")
            forceField.Visible = true
            forceField.Parent = character
        end
    end
end

local function deactivateForceFieldOnPlayer(player)
	print("deactivateForceFieldOnPlayer")
    local character = player.Character
    if character then
		print("character")
        local forceField = character:FindFirstChildOfClass("ForceField")
        if forceField then
            forceField.Visible = false
            forceField:Destroy()
			print("forceField:Destroy")
        end
    end
end

local function proximityPromptFunctionality(item)
    if item.Name ~= "ATM" then return end
    local proximityPrompt = item:FindFirstChildOfClass("ProximityPrompt")
    if proximityPrompt then
        proximityPrompt.Triggered:Connect(function(player)
            OpenBankomat:FireClient(player, proximityPrompt)
            activateForceFieldOnPlayer(player)
        end)
    end
end

if ATMs then
    ATMs.ChildAdded:Connect(proximityPromptFunctionality)
    for _, ATM in ipairs(ATMs:GetChildren()) do
        proximityPromptFunctionality(ATM)
    end
else
    warn("ATMs not found in Workspace!")
end

CloseBankomat.OnServerEvent:Connect(function(player)
    deactivateForceFieldOnPlayer(player)
end)

example video


Thanks

1 Like

Gamma version, Using a script in a game

1 Like