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")'
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)
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.