Edge not working

Hello,
image

script.Parent.RemoteEvent.OnServerEvent:Connect(function(p,t,s)
	if t~=nil then
		if t.Name=="Farmland" then
			if p.leaderstats.Job.Value=="Farmer" then
				if t.Cooldown.Value==false then
					local se = Instance.new("Part",workspace)
					local raycastparams = RaycastParams.new()
					raycastparams.FilterDescendantsInstances(t)
					raycastparams.FilterType = Enum.RaycastFilterType.Whitelist
					if (p.Character.HumanoidRootPart.Position-workspace:Raycast(p.HumanoidRootPart.Position,t.Position,raycastparams).Position).Magnitude<5 then
						s.Pitchfork.grass:Play()
						t.Cooldown.Value = true
						if t.Level.Value>=2 then
							t.Level.Value = 0
							p.leaderstats.Money.Value += 1
						else
							t.Level.Value += 1
						end
						wait(5)
						t.Cooldown.Value = false
					end
				end
			end
		end
	end
end)
-- t is target object
-- s is the tool object
-- p is the player

Hello,

image

Hello,
It appears my initial problem has been solved, but I am faced with yet another problem that completely breaks the script.
In the developer hub, it shows that raycastresults have a .Position property which is the vector3 of the position at which the ray hits, but as seen here, it gives this error.

Hello,
Through further testing, it has appeared that the raycastresult is actually nil, causing the position to be nil as well.


Hello,
I have fixed the nil problem by subtracting the destination position by the origin position,
(t.Position-p.Character.HumanoidRootPart.Position)
However, the script does not work in the way I intend it to (the part should be at the edge of the farmland part if the player is out of reach, but all it is doing is spawning the part in the middle of the farmland part.

Here is what I want it to be doing instead.
(t.Position-p.Character.HumanoidRootPart.Position)

Hello,
I am unsure how to refactor variables, however I managed to do it by hand.

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,target,pitchfork)
	if target~=nil then
		if target.Name=="Farmland" then
			if player.leaderstats.Job.Value=="Farmer" then
				if target.Cooldown.Value==false then
					local raycastparams = RaycastParams.new()
					raycastparams.FilterDescendantsInstances = {target}
					raycastparams.FilterType = Enum.RaycastFilterType.Whitelist
					local e = Instance.new("Part",workspace)
					e.Anchored = true
					e.Position = workspace:Raycast(player.Character.HumanoidRootPart.Position,(target.Position-player.Character.HumanoidRootPart.Position),raycastparams).Position
					print((player.Character.HumanoidRootPart.Position-workspace:Raycast(player.Character.HumanoidRootPart.Position,(target.Position-player.Character.HumanoidRootPart.Position),raycastparams).Position).Magnitude)
					if (player.Character.HumanoidRootPart.Position-workspace:Raycast(player.Character.HumanoidRootPart.Position,(target.Position-player.Character.HumanoidRootPart.Position),raycastparams).Position).Magnitude<5 then
						s.Pitchfork.grass:Play()
						target.Cooldown.Value = true
						if target.Level.Value>=2 then
							target.Level.Value = 0
							player.leaderstats.Money.Value += 1
						else
							target.Level.Value += 1
						end
						wait(5)
						target.Cooldown.Value = false
					end
				end
			end
		end
	end
end)

Hello,
I believe refactoring the variables are unnecesary because p is player, and p is the first letter in player, and t is target, and t is the first letter in target, and s is special, and s is the first letter in special.

Hello,
In addition, I commented on the original script telling you what the variables represent.
Naming the variables words wastes more time and is unncesary.

Hello,
It seems my problem has been solved.

script.Parent.RemoteEvent.OnServerEvent:Connect(function(p,t,s)
	if t~=nil then
		if t.Name=="Farmland" then
			if p.leaderstats.Job.Value=="Farmer" then
				if t.Cooldown.Value==false then
					local raycastparams = RaycastParams.new()
					raycastparams.FilterDescendantsInstances = {t}
					raycastparams.FilterType = Enum.RaycastFilterType.Whitelist
					local e = Instance.new("Part",workspace)
					e.Anchored = true
					e.Position = workspace:Raycast(Vector3.new(p.Character.HumanoidRootPart.Position.X,t.Position.Y,p.Character.HumanoidRootPart.Position.Z),(t.Position-Vector3.new(p.Character.HumanoidRootPart.Position.X,t.Position.Y,p.Character.HumanoidRootPart.Position.Z)),raycastparams).Position
					print((p.Character.HumanoidRootPart.Position-workspace:Raycast(p.Character.HumanoidRootPart.Position,(t.Position-p.Character.HumanoidRootPart.Position),raycastparams).Position).Magnitude)
					if (p.Character.HumanoidRootPart.Position-workspace:Raycast(p.Character.HumanoidRootPart.Position,(t.Position-p.Character.HumanoidRootPart.Position),raycastparams).Position).Magnitude<5 then
						s.Pitchfork.grass:Play()
						t.Cooldown.Value = true
						if t.Level.Value>=2 then
							t.Level.Value = 0
							p.leaderstats.Money.Value += 1
						else
							t.Level.Value += 1
						end
						wait(5)
						t.Cooldown.Value = false
					end
				end
			end
		end
	end
end)

Hello,
Tabs are a natural thing, and beautifies the code.

1 Like