script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - script.Parent.Handle.Position)*300,raycastParams)
if raycastResult then
local hitpos = raycastResult.Position
local hitPart = raycastResult.Instance
local attach = Instance.new("Attachment")
attach.WorldPosition = raycastResult.Position
attach.Parent = hitPart
attach.Visible = true
local balloon = game.ReplicatedStorage.Balloon:Clone()
balloon.Parent = game.Workspace
balloon.PrimaryPart.Position = hitpos
balloon.RopeConstraint.Attachment1 = attach
local model = hitPart:FindFirstAncestorOfClass("Model")
if model then
if model:FindFirstChild("Humanoid") then
model.Humanoid.Health -= 30
end
end
end
end)
Can someone help me? What I want to accomplish is a gun and wherever you click/shoot an attachment will spawn/be created there and then I will attach a balloon to that attachment (I can do the balloon thing with a simple rope constraint) Any and all help is appreciated!
It looks like your script is already partially achieving what you want. To summarize, the script creates a raycast from the gun to the location the player clicks on, then it creates an attachment at the hit position and attaches a balloon to it.
If you want to modify the script to make it more accurate or to add additional features, here are a few suggestions:
Adjust the range of the raycast In your script, the raycast extends 300 studs from the gun. You can adjust this value to make the raycast longer or shorter depending on the needs of your game.
Add a visual effect to the gun when firing To make the gun feel more satisfying to use, you can add a muzzle flash or other visual effect to the gun when it fires.
Add a sound effect to the gun when firing Similarly, you can add a sound effect to the gun to make it feel more impactful.
Add a cooldown or reload time to the gun To prevent players from spamming the gun, you can add a cooldown or reload time between shots.
Here’s an updated version of your script that includes some of these modifications:
local gun = script.Parent
local debounce = false
local function onFire(player, mousePos)
if debounce then return end
debounce = true
-- play a sound effect
local sound = gun:FindFirstChild("GunSound")
if sound then
sound:Play()
end
-- create a muzzle flash
local muzzleFlash = gun:FindFirstChild("MuzzleFlash")
if muzzleFlash then
local effect = muzzleFlash:Clone()
effect.Parent = gun
effect.Enabled = true
wait(0.1)
effect:Destroy()
end
-- create the raycast
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(gun.Handle.Position, (mousePos - gun.Handle.Position).Unit * 1000, raycastParams)
if raycastResult then
-- create the attachment
local attach = Instance.new("Attachment")
attach.WorldPosition = raycastResult.Position
attach.Parent = raycastResult.Instance
attach.Visible = true
-- create the balloon
local balloon = game.ReplicatedStorage.Balloon:Clone()
balloon.Parent = game.Workspace
balloon.PrimaryPart.Position = raycastResult.Position
balloon.RopeConstraint.Attachment1 = attach
-- damage the hit part
local hitPart = raycastResult.Instance
local model = hitPart:FindFirstAncestorOfClass("Model")
if model then
local humanoid = model:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(30)
end
end
end
wait(0.5)
debounce = false
end
gun.Fire.OnServerEvent:Connect(onFire)
This version of the script includes a cooldown between shots by setting a debounce variable that prevents the function from being called again until the cooldown is finished. It also includes code to play a sound effect and create a muzzle flash when the gun is fired.
-- Define the gun's barrel attachment
local barrelAttachment = script.Parent.BarrelAttachment
-- Connect the function to the Fire event
script.Parent.Fire.OnServerEvent:Connect(function(player, mousePos)
-- Set up the raycast parameters
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
-- Calculate the direction of the ray from the barrel attachment to the mouse position
local rayDirection = (mousePos - barrelAttachment.WorldPosition).Unit
-- Perform the raycast
local raycastResult = workspace:Raycast(barrelAttachment.WorldPosition, rayDirection * 300, raycastParams)
-- Check if the raycast hit something
if raycastResult then
-- Create a new Attachment at the hit position and parent it to the hit part
local hitPos = raycastResult.Position
local hitPart = raycastResult.Instance
local attach = Instance.new("Attachment")
attach.WorldPosition = hitPos
attach.Parent = hitPart
-- Create the balloon and attach it to the hit position
local balloon = game.ReplicatedStorage.Balloon:Clone()
balloon.Parent = game.Workspace
balloon.PrimaryPart.Position = hitPos
balloon.RopeConstraint.Attachment1 = attach
-- Reduce the health of the hit model's humanoid if it has one
local model = hitPart:FindFirstAncestorOfClass("Model")
if model then
local humanoid = model:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health -= 30
end
end
end
end)
This script doesn’t seem to work. I have been having troubles with this. And to provide some more information this script (I changed it from last time)
script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position) * 300, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
local hitPosition = raycastResult.Position
local attach = Instance.new("Attachment")
attach.CFrame = CFrame.new(hitPosition)
attach.Parent = hitPart
print("Hit Position:", hitPosition)
print("Attachment Position:", attach.WorldPosition)
local balloon = game.ReplicatedStorage.Balloon:Clone()
balloon.Parent = workspace
balloon:SetPrimaryPartCFrame(CFrame.new(hitPosition))
balloon.RopeConstraint.Attachment1 = attach
local model = hitPart:FindFirstAncestorOfClass("Model")
if model then
local humanoid = model:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(30)
end
end
end
end)
works un anchored parts but not on unanchored parts.
I have been stuck with this for a while. This is my newest script.
script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position) * 300, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
local hitPosition = raycastResult.Position
local attach = Instance.new("Attachment")
attach.CFrame = CFrame.new(hitPosition)
attach.Parent = hitPart
print("Hit Position:", hitPosition)
print("Attachment Position:", attach.WorldPosition)
local balloon = game.ReplicatedStorage.Balloon:Clone()
balloon.Parent = workspace
balloon:SetPrimaryPartCFrame(CFrame.new(hitPosition))
balloon.RopeConstraint.Attachment1 = attach
local model = hitPart:FindFirstAncestorOfClass("Model")
if model then
local humanoid = model:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(30)
end
end
end
end)
It works when i just click on the baseplate or something anchored but not when i click on something anchored. This the hit position and attachment position for an anchored part 06:56:39.499 Hit Position: -1.3556596040725708, 0, -10.056815147399902 - Server - Server2.0:16
06:56:39.499 Attachment Position: -1.3556596040725708, -10, -10.056815147399902 - Server - Server2.0:17 and here is an unanchored part 06:56:58.299 Hit Position: -29.928237915039062, 3.2020673751831055, -39.248931884765625 - Server - Server2.0:16
06:56:58.299 Attachment Position: -70.52479553222656, 11.900918960571289, -95.80228424072266 - Server - Server2.0:17
this should be 1st (mousePos - script.Parent.Handle.Position).Unit idk why but its most likely used on ray jobs and what is exactly going wrong when you shoot a unanchored part? a file of what you want to achieve will be awsome i see you are making a gun where you want to kill people and add some kind of bloon effect im guessing the bloon spawn is wrong but tell me further details like what is wrong what is the problem …