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

Attempting to index nil with 'Character' ?

So I am working on a Dice Rolling script, essentially you press a TextButton and it will run the function.

The function is the dice rolling script, and uses a BillBoard gui in ReplicatedFirst, adds a random number in there, clones it and then places it in the Character's HumanoidRootPart

But I keep getting an error Attempting to index nil with Character

This is also a LocalScript, here it is The problem is on line 16

local rep = game:GetService("ReplicatedFirst")
local gui = rep:WaitForChild("d20gui")
local db = game:GetService("Debris")
local OpenMenu = script.Parent

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

OpenMenu.MouseButton1Click:Connect(function(p)
	local toomany = false
	local count = 0
	math.randomseed(tick())
	local charch = p.Character:GetChildren()
	local existingd20tabs = {}
	for i, v in pairs(charch) do
		if v.Name == "d20gui" then
			count = count + 1
			table.insert(existingd20tabs, v)
		end
	end
	
	if count < 5 then
		for i, v in pairs(existingd20tabs) do
			v.StudsOffsetWorldSpace = v.StudsOffsetWorldSpace + Vector3.new(0, 2, 0)
		end
		local pgui = gui:Clone()
		pgui.Parent = p.Character
		pgui.Adornee = p.Character.HumanoidRootPart
		local text = pgui:WaitForChild("Frame"):WaitForChild("number")
		text.Text = math.random(1, 20)
		for i = 1, 17 do
			local modifier = math.ceil(i / 5)
			local newnumber = math.random(1, 20)
			--print(0.05 * modifier)
			wait(0.05 * modifier)
			text.Text = newnumber
		end
		wait(5)
		pgui:Destroy()
	end
end
)

3 comments

0

Could you put your code inside a code block in this question? Edit the question and use the "Lua" icon to create a code block, and put the code between the ~ lines.

0

well where is "p" defined

0

There we go I added the lua block Im sorry about that

1 answer

✓ Accepted answer

Theres nothing in this script defining "p" so change it to this

local rep = game:GetService("ReplicatedFirst")
local gui = rep:WaitForChild("d20gui")
local db = game:GetService("Debris")
local OpenMenu = script.Parent

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

OpenMenu.MouseButton1Click:Connect(function()
    local toomany = false
    local count = 0
    math.randomseed(tick())
    local charch = player.Character:GetChildren()
    local existingd20tabs = {}
    for i, v in pairs(charch) do
        if v.Name == "d20gui" then
            count = count + 1
            table.insert(existingd20tabs, v)
        end
    end

    if count < 5 then
        for i, v in pairs(existingd20tabs) do
            v.StudsOffsetWorldSpace = v.StudsOffsetWorldSpace + Vector3.new(0, 2, 0)
        end
        local pgui = gui:Clone()
        pgui.Parent = player.Character
        pgui.Adornee = player.Character.HumanoidRootPart
        local text = pgui:WaitForChild("Frame"):WaitForChild("number")
        text.Text = math.random(1, 20)
        for i = 1, 17 do
            local modifier = math.ceil(i / 5)
            local newnumber = math.random(1, 20)
            --print(0.05 * modifier)
            wait(0.05 * modifier)
            text.Text = newnumber
        end
        wait(5)
        pgui:Destroy()
    end
end
)

5 comments

0

also you cant get player through mousebutton1click

0

ahhh that seemed to do it, i thought that adding a "p" parameter to the function would allow me to call that function by just using "p"

0

make sure to accept it as the answer if it helped

0

just did! thank you so much for pointing that out

0

no problem

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