I'm trying to make an animated E to open door script, but when I try to press e, on the output, it displays the error: "Workspace.Doors.Model.Main:5: attempt to index nil with 'Character'". Here is the script below:
local Can = true local Open = false script.Parent.Event.OnServerEvent:Connect(function(Player) local Mag = (script.Parent.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude if Mag <= script.Parent.Range.Value then if Can then Can = false if Open == false then local Finish = script.Parent.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0) for i = 0,1,.1 do local CFM = script.Parent.PrimaryPart.CFrame:lertp(Finish,i) script.Parent:SetPrimaryPartCFrame(CFM) wait() end Open = true else Open = false local Finish = script.Parent.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0) for i = 0,1,.1 do local CFM = script.Parent.PrimaryPart.CFrame:lertp(Finish,i) script.Parent:SetPrimaryPartCFrame(CFM) wait() end end Can = true end end end)
According to the error, this is the line that triggers the error:
local Mag = (script.Parent.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
This is a similar post to another post I made, which I tried to fix but did not work. Can someone help me out why this occurs? Thanks!
You can not use localplayer on server-sided scripts, rather try making use of the Player parameter with it's set argument. So remove the game.Players.LocalPlayer and do
local Mag = (script.Parent.Center.Position - Player.Character.HumanoidRootPart.Position).Magnitude
Hopefully this helps
@DeUltimate23 it does not fix the issue, the output is not showing any errors for the door, but the door does not open. Here are the scripts used for the door:
LocalScript, located in StarterGui, ScreenGui:
local WaitLocalPlayer = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart") local UIS = game:GetService("UserInputService") UIS.InputBegan:Connect(function(i, g) if i.UserInputType == Enum.UserInputType.Keyboard then if i.KeyCode == Enum.KeyCode.E then for _, Door in pairs(workspace.Doors:GetChildren()) do local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude if Mag <= Door.Range.Value then Door.Event:FireServer() break end end end end end) while true do script.Parent.ImageLabel.Visible = false for _, Door in pairs(workspace.Doors:GetChildren()) do local Mag = (Door.Center.Position-game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude if Mag <= Door.Range.Value then local D3ToD2 = workspace.CurrentCamera:WorldToScreenPoint(Door.Center.Position) script.Parent.ImageLabel.Visible = true script.Parent.ImageLabel.Position = UDim2.new(0,D3ToD2.X,0,D3ToD2.Y,0) break end end wait() end
Normal script, named "Main", located in game.Workspace.Doors.Model
local Can = true local Open = false script.Parent.Event.OnServerEvent:Connect(function(Player) local Mag = (script.Parent.Center.Position - Player.Character.HumanoidRootPart.Position).Magnitude if Mag <= script.Parent.Range.Value then if Can then Can = false if Open == false then local Finish = script.Parent.PrimaryPart.CFrame*CFrame.Angles(0,math.rad(90),0) for i = 0,1,.1 do local CFM = script.Parent.PrimaryPart.CFrame:lertp(Finish,i) script.Parent:SetPrimaryPartCFrame(CFM) wait() end Open = true else Open = false local Finish = script.Parent.PrimaryPart.CFrame*CFrame.Angles(0,-math.rad(90),0) for i = 0,1,.1 do local CFM = script.Parent.PrimaryPart.CFrame:lertp(Finish,i) script.Parent:SetPrimaryPartCFrame(CFM) wait() end end Can = true end end end)