Having Problems with raycasting

Hi Everyone!
This is my first time using RayCast and i want to make random spawning orb wich gives xp to my pets but it wont work this is my script
This script is in orb and its server-sided

local rayOrigin = script.Parent.Position
local rayDirection = Vector3.new(0, -100, 0)

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

local DB = false
local XPgain = 25
local Delay = 5
local randomz = math.random(-22,60)
local randomx = math.random(-58,58)
local ts = game:GetService("TweenService")
local info = TweenInfo.new(0.5)
local goal = {
	Size = script.Parent.Size + Vector3.new(1,1,1);
	Transparency = 1;
}
local partclone = script.Parent:Clone()
partclone.Script:Destroy()
local tween = ts:Create(partclone,info,goal)
script.Parent.Touched:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	if Hum then
		if not DB then
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			for i,v in pairs(Player.Pets:GetChildren()) do
				if v.Equipped.Value == true then
					v.TotalXP.Value = v.TotalXP.Value + XPgain
				end
			end
			for i,v in pairs(workspace.PlayerPets:FindFirstChild(Player.Name):GetChildren()) do
				spawn(function()
					local Clone = game.ReplicatedStorage.Pets.AddXPdisplay:Clone()
					Clone.Parent = v.PrimaryPart
					for i = 1,25 do
						Clone.StudsOffset = Clone.StudsOffset + Vector3.new(0, .04, 0)
						Clone.TextLabel.TextTransparency = Clone.TextLabel.TextTransparency + .04
						wait(.02)
					end
					Clone:Destroy()
				end)
			end
			DB = true
			tween:Play()
			if raycastResult then
				local hitPart = raycastResult.Instance
				script.Parent.Position = Vector3.new(randomx,hitPart.Position.Y + Vector3.new(0,3,0),randomz)
			end
			script.Parent.Transparency = 1
			wait(Delay)
			DB = false
			partclone:Destroy()
			script.Parent.Transparency = 0
		end
	end
end)

Any Help is appreciated Thanks!

1 Like

I dont understand why you use RayCast For this , anyways. Add Some Print statements inside the if raycastresult then and try if it prints.

I use raycast because there is enviroment and enviroment is collidable so if it hits the enviroment it appears on it

This statement here is Wrong You can’t add Vector3 to the Y axis alone so what you can do is

script.Parent.Position = Vector3.new(randomx,hitPart.Position.Y,randomz)+ Vector3.new(0,3,0)

2 Likes

The thing that @Razor_IB did is correct. Also does it show an error in the output?

Its not working and not showing up any error btw when i added print statement it even didnt print the hitpart i addewd print(hitpart.Name)

Maybe change rayDirection to this:

local rayDirection = (CFrame.new(rayOrigin)).UpVector * -100

Your Ray might not be hitting any parts , so you have to change your ray direction to something that travels much further , what @DeFunnyPerson Showed should work .