Hello all, I am quite new to scripting and I am trying to modify a part to, instead of equip a hat when touched, to equip the hat when the proximity prompt is triggered. I am new to scripting and am having trouble fully wrapping my head around this. Would anyone be able to give me advice on what I am doing wrong and point me in the right direction? Thanks all.
local ProximityPrompt = script.Parent
local hit = ProximityPrompt.Triggered
--Main Function
ProximityPrompt.Triggered:Connect(function(Player)
if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Helmet") == nil then
local g = script.Parent.Parent.Parent.Helmet:clone()
g.Parent = hit.Parent
local C = g:GetChildren()
for i=1, #C do
if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
--Welding
local W = Instance.new("Weld")
W.Part0 = g.Middle
W.Part1 = C[i]
local CJ = CFrame.new(g.Middle.Position)
local C0 = g.Middle.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = g.Middle
end
local Y = Instance.new("Weld")
Y.Part0 = hit.Parent.Head
Y.Part1 = g.Middle
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end
local h = g:GetChildren()
for i = 1, # h do
h[i].Anchored = false
h[i].CanCollide = false
end
end
end
script.Parent.Triggered:connect(onTouched)
hit is the part that gets touched, so hit.Parent is trying to get the character of the player because the character is the parent of the body parts
make a variable that holds the players character and then replace hit.Parent with that variable
also you have to get the character each time the prompt is triggered (in case it dies and a new one is made) so put it right after the triggered event
local character = Player.Character
-- (with .Touched) hit.Parent:FindFirstChild("Humanoid")
-- (with proximity prompt) character:FindFirstChild("Humanoid")
Its literally just the way you do the function, replaced the stuff at the end with end) because you already connected the function at the top of the script… then connected it at the bottom again without closing it
local ProximityPrompt = script.Parent
local hit = ProximityPrompt.Triggered
--Main Function
ProximityPrompt.Triggered:Connect(function(Player)
if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Helmet") == nil then
local g = script.Parent.Parent.Parent.Helmet:clone()
g.Parent = hit.Parent
local C = g:GetChildren()
for i=1, #C do
if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
--Welding
local W = Instance.new("Weld")
W.Part0 = g.Middle
W.Part1 = C[i]
local CJ = CFrame.new(g.Middle.Position)
local C0 = g.Middle.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = g.Middle
end
local Y = Instance.new("Weld")
Y.Part0 = hit.Parent.Head
Y.Part1 = g.Middle
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end
local h = g:GetChildren()
for i = 1, # h do
h[i].Anchored = false
h[i].CanCollide = false
end
end
end)
As for the final line script.Parent.Triggered:connect(onTouched) shouldn’t that cause a function named onTouched to fire?
Why would you be calling onTouched for a ProximityPrompt anyway? if the .Triggered function is already firing you won’t need a Touched event.
EDIT, just saw that @Aerophagia covered this already.
Workspace.Helmet.Part.ProximityPrompt.Head:3: attempt to index nil with 'Character'
This is the error I am now getting… anybody know what this means? I tried to change the code to modify it so it will have to wait for the character to be added, but am still met with error.
Like I said, that first error isn’t from this script. It’s telling you that line 3 of the script is erroring. You aren’t showing that script here.
local ProximityPrompt = script.Parent
local hit = ProximityPrompt.Triggered
local character = player.CharacterAdded:wait()
--Main Function
ProximityPrompt.Triggered:Connect(function(Player)
if character:findFirstChild("Humanoid") ~= nil and character:findFirstChild("Helmet") == nil then
local g = script.Parent.Parent.Parent.Helmet:clone()
g.Parent = character
local C = g:GetChildren()
for i=1, #C do
if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
--Welding
local W = Instance.new("Weld")
W.Part0 = g.Middle
W.Part1 = C[i]
local CJ = CFrame.new(g.Middle.Position)
local C0 = g.Middle.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = g.Middle
end
local Y = Instance.new("Weld")
Y.Part0 = character.Head
Y.Part1 = g.Middle
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end
local h = g:GetChildren()
for i = 1, # h do
h[i].Anchored = false
h[i].CanCollide = false
end
end
end)
this error would mean the Player is nil, because you did hit.Parent instead of Player.Character:FindFirstChild(“Humanoid”)
local ProximityPrompt = script.Parent
--- REMOVING THE HIT VARIABLE, its useless
--Main Function
ProximityPrompt.Triggered:Connect(function(Player)
local Char = Player.Character -- NEW CHARACTER VARIABLE
if Char:FindFirstChild("Humanoid") ~= nil and Char:FindFirstChild("Helmet") == nil then -- THIS LINE WAS INCORRECT
local g = script.Parent.Parent.Parent.Helmet:clone()
g.Parent = hit.Parent
local C = g:GetChildren()
for i=1, #C do
if C[i].className == "Part" or C[i].className == "UnionOperation" or C[i].className == "MeshPart" then
--Welding
local W = Instance.new("Weld")
W.Part0 = g.Middle
W.Part1 = C[i]
local CJ = CFrame.new(g.Middle.Position)
local C0 = g.Middle.CFrame:inverse()*CJ
local C1 = C[i].CFrame:inverse()*CJ
W.C0 = C0
W.C1 = C1
W.Parent = g.Middle
end
local Y = Instance.new("Weld")
Y.Part0 = hit.Parent.Head
Y.Part1 = g.Middle
Y.C0 = CFrame.new(0, 0, 0)
Y.Parent = Y.Part0
end
local h = g:GetChildren()
for i = 1, # h do
h[i].Anchored = false
h[i].CanCollide = false
end
end
end)