Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index nil with Character?

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.

game.ReplicatedStorage.DefStand.Summon.OnServerEvent:Connect(function()
	local sound = game.Workspace.DS_summon
	local Stand = game.ServerStorage.DefaultStand:Clone()
	local StandModel = Stand.Dummy:Clone()
	local debounce = false
	
	sound:Play()
	StandModel.Parent = game.Workspace
	if StandModel then
		local function summonWeld()
			local player = game.Players.LocalPlayer
			local char = player.Character or player.CharacterAdded:Wait() --Error here.
			local weld = game.Workspace.Weld:Clone()
			
			weld.Part0 = char
			weld.Part1 = StandModel
		end
		summonWeld()
	end
end)

1 answer

✓ Accepted answer

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:

game:GetService("ReplicatedStorage").DefStand.Summon.OnServerEvent:Connect(function(player)
    local sound = workspace.DS_summon
    local Stand = game:GetService("ServerStorage").DefaultStand:Clone()
    local StandModel = Stand.Dummy:Clone()
    local debounce = false

    sound:Play()
    StandModel.Parent = workspace
    if StandModel then
        local function summonWeld()
			local char = player.Character or player.CharacterAdded:Wait() --Error here.
            local weld = game.Workspace.Weld:Clone()
		weld.C0 = char.HumanoidRootPart.CFrame:Inverse()
		weld.C1 = StandModel.HumaniodRootPart.CFrame:Inverse()
            weld.Part0 = char.HumanoidRootPart
            weld.Part1 = StandModel
        end
        summonWeld()
    end
end)

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.

2 comments

0

you don't need to set the c0 or c1 properties anymore if you use constraints.

0

You are a mad-lad, thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.