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

Attempt to index a nil value. What's wrong?

Asked by 4 years ago

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)

0
Could you provide the line for the error? Psudar 882 — 4y
1
The model is actually an union part, so position must work, right? JexaButterscotch 73 — 4y

2 answers

Log in to vote
1
Answered by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years ago

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.

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. Psudar 882 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
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)

0
ah yeah, too bad the humanoidrootpart isnt located in the player. thats still going to error. Psudar 882 — 4y
0
It should work now Vinceberget 1420 — 4y

Answer this question