Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Attempt to index nil with Character?

Asked by 4 years ago

Currently, I am creating an engine that can be used in JoJo's Bizarre Adventure games. However, since I am at a somewhat beginner to intermediate level, I am having trouble referencing the player's humanoidrootpart so it can be welded to a dummy. Any tips on how to get around the index nil error? As an extra note, this is on a server script triggered by an event.

01game.ReplicatedStorage.DefStand.Summon.OnServerEvent:Connect(function()
02    local sound = game.Workspace.DS_summon
03    local Stand = game.ServerStorage.DefaultStand:Clone()
04    local StandModel = Stand.Dummy:Clone()
05    local debounce = false
06 
07    sound:Play()
08    StandModel.Parent = game.Workspace
09    if StandModel then
10        local function summonWeld()
11            local player = game.Players.LocalPlayer
12            local char = player.Character or player.CharacterAdded:Wait() --Error here.
13            local weld = game.Workspace.Weld:Clone()
14 
15            weld.Part0 = char
16            weld.Part1 = StandModel
17        end
18        summonWeld()
19    end
20end)

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

Hello. Regular/ServerScripts cannot detect the LocalPlayer from game.Players.LocalPlayer so it returns nil and it will error when trying to index something with it. Instead, use the automatic player parameter of OnServerEvent. Try this:

01game:GetService("ReplicatedStorage").DefStand.Summon.OnServerEvent:Connect(function(player)
02    local sound = workspace.DS_summon
03    local Stand = game:GetService("ServerStorage").DefaultStand:Clone()
04    local StandModel = Stand.Dummy:Clone()
05    local debounce = false
06 
07    sound:Play()
08    StandModel.Parent = workspace
09    if StandModel then
10        local function summonWeld()
11            local char = player.Character or player.CharacterAdded:Wait() --Error here.
12            local weld = game.Workspace.Weld:Clone()
13        weld.C0 = char.HumanoidRootPart.CFrame:Inverse()
14        weld.C1 = StandModel.HumaniodRootPart.CFrame:Inverse()
15            weld.Part0 = char.HumanoidRootPart
16            weld.Part1 = StandModel
17        end
18        summonWeld()
19    end
20end)

Also, welds don't work if they're a child of a Model. Instead, use the character's HumanoidRootPart. Also, you forgot the C0 and C1 properties of the weld.

Please accept and upvote this answer if it helped.

0
you don't need to set the c0 or c1 properties anymore if you use constraints. SteamG00B 1633 — 4y
0
You are a mad-lad, thank you so much! Crimsonknightzone 38 — 4y
Ad

Answer this question