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 a nil value. What's wrong?

I'm trying to make a gui button that when pressed, will spawn a model at the player, however it doesn't work.

game.ReplicatedStorage.RemoteEvents.KillerTrap.OnServerEvent:Connect(function(player, partfolder, green)
	if player and partfolder then
		local newPart = script.StickModel:Clone()
		newPart.Name = player.Name.."'s Trap"
		newPart.Positon = player:FindFirstChild("HumanoidRootPart").Position
		newPart.Orietation = player:FindFirstChild("HumanoidRootPart").Orientation
		wait(3)
		newPart.Code.Disabled = false
	end
end)

2 comments

0

Could you provide the line for the error?

1

The model is actually an union part, so position must work, right?

2 answers

✓ Accepted answer

Okay, just to iron out a few potential problems, im going to tell the script to halt until the remote events and such exist.


--//Services

local replicatedStorage = game:GetService("ReplicatedStorage")

--//instances

local remoteEvents = replicatedStorage:WaitForChild("RemoteEvents") 
local killerTrapRemote = remoteEvents:WaitForChild("KillerTrap")

--//Functions

local function giveModel(player, partFolder, green)
if player and parFolder then
		local newPart = script:WaitForChild("StickModel"):Clone()
		newPart.Name = player.Name.."'s Trap"
		newPart.CFrame = player.Character:FindFirstChild("HumanoidRootPart").CFrame
		newPart.Parent = workspace
		wait(3)
		newPart.Code.Disabled = false
	end
end

--//Connections

killerTrapRemote.OnServerEvent:Connect(giveModel)

in your script, you werent accessing the player's character for the HumanoidRootPart, so I think that was the initial problem. To prevent any further issues, I added some functions that will make the script halt until they appear, so that you can avoid future errors similar to this.

When you use :FindFirstChild("HumanoidRootPart") on the player, it returns nil, because the player doesnt have a child called HumanoidRootPart. The character does, however.

1 comment

0

oh yeah and i added the CFrame instead of using the position, since you wanted the orientation to be the same as well. Cheers! Gl.

game.ReplicatedStorage.RemoteEvents.KillerTrap.OnServerEvent:Connect(function(player, partfolder, green)
    if player and partfolder then
        local newPart = script.StickModel:Clone()
        newPart.Name = player.Name.."'s Trap"
        newPart.Position = player.Character:FindFirstChild("HumanoidRootPart").Position -- fixed typo
        newPart.Orientation = player.Character:FindFirstChild("HumanoidRootPart").Orientation -- fixed typo here too
	newPart.Parent = workspace -- sets parent (missed part)
        wait(3)
        newPart.Code.Disabled = false
    end
end)

2 comments

0

ah yeah, too bad the humanoidrootpart isnt located in the player. thats still going to error.

0

It should work now

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