Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Why does my output say "Workspace.Doors.Model.Main:5: attempt to index nil with 'Character'"?

Asked by 8 days ago
Edited 8 days ago

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!

2 answers

Log in to vote
0
Answered by 8 days ago

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

Ad
Log in to vote
0
Answered by 8 days ago
Edited 8 days ago

@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)
0
Maybe write Lerp instead of lertp? DeUltimate23 59 — 8d
0
And also try to use TweenService as well and see if that is better DeUltimate23 59 — 8d

Answer this question