I’m trying to make a gun, but when its parented to Workspace, even though I set the position, it just starts at the center of the Baseplate. I tried searching it up (Yes I used keywords instead of the entire phrase) I found similar things, but none of them had the same problem that I did.
Code
Shoot.OnServerEvent:Connect(function(Player,MouseHit,Gun)
local Character = Player.Character or Player.CharactedAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
if Gun.Name == "Desert Eagle" then
local Bullet = Instance.new("Part",workspace)
Bullet.Size = Vector3.new(1,1,1)
Bullet.CFrame = Gun.Handle.CFrame
local BodyVelocity = Instance.new("BodyVelocity",Bullet)
BodyVelocity.Velocity = (MouseHit.LookVector*Vector3.new(math.random(-1.2,1.2),1,1)) * 1000 -- Randomness added in there to make the gun less accurate
BodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
local hitbox = Hitbox(Bullet)
hitbox.Touched:Connect(function(Hit)
if IsCharacter(Hit.Parent) and Hit.Parent.Name ~= Player.Name then
local Humanoid = Hit.Parent:WaitForChild("Humanoid")
if Hit.Name == "Head" then
Humanoid:TakeDamage(50)
else
Humanoid:TakeDamage(25)
end
end
end)
end
end)
You should always Parentlast after setting your Properties, the second parameter of Instance.new() is not recommended to use
Shoot.OnServerEvent:Connect(function(Player,MouseHit,Gun)
local Character = Player.Character --or Player.CharactedAdded:Wait() There's no need to wait for the Character to load
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
if Gun.Name == "Desert Eagle" then
local Bullet = Instance.new("Part")
Bullet.Size = Vector3.new(1,1,1)
Bullet.CFrame = Gun.Handle.CFrame
Bullet.Parent = workspace
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.Velocity = (MouseHit.LookVector*Vector3.new(math.random(-1.2,1.2),1,1)) * 1000 -- Randomness added in there to make the gun less accurate
BodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
BodyVelocity.Parent = Bullet
local hitbox = Hitbox(Bullet)
hitbox.Touched:Connect(function(Hit)
if IsCharacter(Hit.Parent) and Hit.Parent.Name ~= Player.Name then
local Humanoid = Hit.Parent:WaitForChild("Humanoid")
if Hit.Name == "Head" then
Humanoid:TakeDamage(50)
else
Humanoid:TakeDamage(25)
end
end
end)
end
end)
shoot.OnServerEvent:Connect(function(player,mouseHit,gun)
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
if gun.Name == "Desert Eagle" then
local bullet = Instance.new("Part")
bullet.Size = Vector3.new(1,1,1)
bullet.CFrame = humanoidRootPart.CFrame
bullet.Parent = workspace
local BodyVelocity = Instance.new("BodyVelocity",bullet)
BodyVelocity.Velocity = (mouseHit.LookVector+getInaccuracy(0.5)) * 1000
BodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
local hitbox = Hitbox(bullet)
hitbox.Touched:Connect(function(hit)
if isCharacter(hit.Parent) and hit.Parent.Name ~= player.Name then
local Humanoid = hit.Parent:WaitForChild("Humanoid")
if hit.Name == "Head" then
Humanoid:TakeDamage(50)
else
Humanoid:TakeDamage(25)
end
end
end)
end
end)