My script is not working

My script is supposed to check when a button is pressed and give them a force field

local UserInputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local Players = game:GetService("Players")
local accessory = game.ServerStorage:WaitForChild("ForceField")
local character = plr.LocalPlayer.Character

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.X then
		local plr = game.Players:FindFirstChild(character.Name)
		local clone = accessory:Clone()
		clone.Parent = character.HumanoidRootPart
	end
end)

In the output it says

Infinite yield possible on 'ServerStorage:WaitForChild("ForceField")'

You can’t access the server storage from the client. Use a remotevent to tell the server to give them a force field.

1 Like

You cannot access the server storage in a local script that can only be accessed through the server, you have to put it in ReplicatedStorage.

1 Like

Ok thank you both for replying I will make the script call a function and another script in serverscriptservice receive it and run the code

Well now I have done it and this is the server script

local plr = game.Players.LocalPlayer
local Players = game:GetService("Players")
local accessory = game.ServerStorage:WaitForChild("ForceField")
local character = plr.LocalPlayer.Character
local remote = game.ReplicatedStorage.RemoteEvent.OnServerEvent

remote:Connect(function()
	local plr = game.Players:FindFirstChild(character.Name)
	local clone = accessory:Clone()
	clone.Parent = character.HumanoidRootPart
end)

And in the output It says

ServerScriptService.Script:4: attempt to index nil with 'LocalPlayer'

You should use Remote Events for stuff like that, since this kind of stuff is easy I’ll give you some code.

--LOCAL SCRIPT
local UserInputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local character = plr.Character
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")

if not character or not character.Parent then
	character = plr.CharacterAdded:Wait()
end

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
	if GameProcessed == true then return end -- If you're typing in the chat it will not trigger any of the if statement below this line of code.
	
	if Input.KeyCode == Enum.KeyCode.X then
		RemoteEvent:FireServer()
	end
end)

-- SERVER SCRIPT (Create a Normal/Server Script in ServerScriptService and paste in the code below)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player) -- The player that fired the event is always the first in the parameter.
	local Chr = Player.Character
	
	if not Chr:FindFirstChild("ForceField") then
		local Forcefield = Instance.new("ForceField") -- Do not use the parent array in Instance.new() if you care about performance.
		Forcefield.Parent = Chr
	end
end)
1 Like

Ok thank you much appreciated I will test it out

change the 4th line to

local character = plr.Character

try this bro @jjadtru it should work

It won’t work, local script is basically the client. If you create a instance with a local script, the client will only see that and the server (And all clients in the server) won’t be able to see it.

It does work so… :thinking: :grinning:

If it solved your problem then please mark it as a solution!

yea @jjadtru mark it…