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

Sometimes my script works. Sometimes it doesnt? Sometimes is the main problem

Well i have a script that will work maybe the first time. And i dont think works if you put it on an accessory. And im just so confused. I dont expect an answer. But heres the Pick Up script that barely ever works Script

local event = game.ReplicatedStorage.PickupPlayer
local drop = game.ReplicatedStorage.Drop

event.OnServerEvent:Connect(function(Player, FromP, ToP)
	local RayCast = Ray.new(FromP, (ToP-FromP).unit*100)
	local part = game.Workspace:FindPartOnRay(RayCast, Player.Character, false, true)
	if part then
		print(part)
		if part.Parent:FindFirstChildWhichIsA("Humanoid") or part.Parent.ClassName == "Accessory" then
			local plrchar = Player.Character
			local char = part.Parent or part.Parent.Parent
			local Hum = char:FindFirstChild("Humanoid") --I THINK I HAVE TO CHANGE THIS LINE
				local PickUps = Player:FindFirstChild("leaderstats"):FindFirstChild("Pick Ups")
				local holding = Player.leaderstats:FindFirstChild("Holding")
				if not char:FindFirstChild("Weld") then
					if holding.Value == false then
						Hum.PlatformStand = true --THIS LINE IS ERRORING
						local weld = Instance.new("Weld", plrchar)
						weld.Part0 = char.HumanoidRootPart
						weld.Part1 = Player.Character.HumanoidRootPart
						weld.C0 = char.HumanoidRootPart.CFrame:inverse()
						weld.C1 = Player.Character.HumanoidRootPart.CFrame:inverse()
						char.HumanoidRootPart.Position = Player.Character.HumanoidRootPart.Position + Vector3.new(0, 5, 0)
						holding.Value = true
						PickUps.Value = PickUps.Value + 1
						drop:FireClient(Player, char.Parent)
				end
			end
		end
	end
end)

Feel free to correct me on more effective ways to make it. I have no ideas. I thought this would work. Sometimes i get an error about PlatformStand being nil, So i think i did something wrong with the humanoid variable. Thanks

2 comments

0

Could you explain what the script is trying to do first?

0

Pick up a player, Put them a few studs above you

1 answer

The problem is possible in

local char = part.Parent or part.Parent.Parent

How does the code would know witch one to get? If it hits a char's part, char is the Character or Workspace. And then it may try to get first child Humanoid from Workspace, leading to nil. So now Hum = Humanoid or nil. And in line 17 it would do Humanoid.PlartformStanding or nil.PlatformStanding -- error.

What i do to deal with hit on accessory is

local Hum
if hit.Parent:FindFirstChild("Humanoid") then
	Hum = hit.Parent.Humanoid
elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
	Hum = hit.Parent.Parent.Humanoid
end

Another tip is that the use of the second parameter for parent in Instance.new is deprecated. The game makes lots of effort to listen to this parameter, and the creators advised not to use it anymore. (Instance.new("Weld", dont use this)

More info: https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296

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