Hey guys, what’s up? I got a problem while raycasting, that is a weird problem i think, that is when i shoot my gun, the ray doesnt move with the origin part, it stays in one place i dont know why, and also when i unequip the gun and equip again, the ray doesnt go where the mouse is pointing at. I’ve never seen this problem, will it be a weld problem? thanks in advance guys.
Here i leave some proof so you guys can see my problem.
robloxapp-20200728-0903206.wmv (2.2 MB) robloxapp-20200728-0904117.wmv (3.6 MB)
if you wanna to see my code here it is.
ServerSided script
local shootEvent = game.ReplicatedStorage.Shoot
local tool = script.Parent
local shootsound = tool.ShootSound
local CurrentAmmo = tool.CurrentAmmo
local BulletsFolder = game.Workspace.balas
local debris = game:GetService("Debris")
local function bulletsTrajectory(origin, direction)
local midpoint = origin + direction / 2
local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.BrickColor = BrickColor.new("Cool yellow")
part.CFrame = CFrame.new(midpoint, origin)
part.Size = Vector3.new(0.1,0.1,direction.magnitude)
part.Parent = BulletsFolder
debris:AddItem(part, 0.3)
end
shootEvent.OnServerEvent:Connect(function(player, originPos, destinationPos)
local directionRay = (destinationPos - originPos).Unit * 100
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {tool}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local ResultOfRaycasting = workspace:Raycast(originPos, directionRay, raycastParams)
if ResultOfRaycasting then
local hitpart = ResultOfRaycasting.Instance
if hitpart then
print(hitpart)
end
end
bulletsTrajectory(originPos, directionRay)
end)
Client Script
local player = game.Players.LocalPlayer
local BulletsFolder = game.Workspace.balas
local tool = script.Parent.Parent
local originPosition = tool.Model:WaitForChild("Origin")
local contextactionservice = game:GetService("ContextActionService")
local ShootEvent = game.ReplicatedStorage.Shoot
local RifleGUI = tool.Model.Frame.Ammo
local CurrentAmmo = tool.Model:WaitForChild("CurrentAmmo")
local ShootSound = tool.Model:WaitForChild("ShootSound")
local crosshair = "rbxasset://SystemCursors/Cross"
local PlayerMouse = player:GetMouse()
PlayerMouse.TargetFilter = BulletsFolder
tool.Equipped:Connect(function(mouse)
RifleGUI.Text = CurrentAmmo.Value
RifleGUI.Visible = true
local originPos = originPosition.Position
local destinationPos = mouse.Hit.Position
mouse.Button1Down:Connect(function()
ShootEvent:FireServer(originPos, destinationPos)
end)
end)
tool.Unequipped:Connect(function()
RifleGUI.Visible = false
end)