Hey, thank you for reading and willing to assist me!
I got so far 2 scripts on my testing tool, one is a localscript and one is a normal script.
By using the Localscript it would fire a Event to shoot the bullet. However on this bullet there is sadly no movement on yet and it falls though the ground.
I though personally about raycasting, tweenservice, cframing or may be anything else. But I’m really unsure what the best solution is.
Localscript:
-- Character based locals
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local Head = Character:WaitForChild("Head")
-- Services & Event
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")
local UserInputService = game:GetService("UserInputService")
-- Gun locals
local Handle = script.Parent:WaitForChild("Handle")
local MaterialFolder = script.Parent:WaitForChild("Materials")
local GunGUI = MaterialFolder.Gui:WaitForChild("ScreenGui")
local cooldown = .3
local Order = 1 -- Useful for combos (eg: swing1, swing2 on melee close)
local Debounce = true
local Maxammo = 10
local Ammo = 10
local Reloading = false
local AllowReloading = false
local Mouse = Player:GetMouse()
script.Parent.Equipped:Connect(function()
--RemoteEvent:FireServer("Equiped") -- Unnecessary, only be good if you have some cooldown aswell as equip animation that must be showen ingame?
NewGui = GunGUI:Clone()
NewGui.Parent = Player.PlayerGui
print(NewGui)
NewGui:WaitForChild("TextLabel").Text = Ammo
AllowReloading = true -- we wanna allow reloading when tool is equipped
end)
script.Parent.Unequipped:Connect(function()
--RemoteEvent:FireServer("Deequipped") -- Unnecessary, only be good if you have some cooldown aswell as Deequipped animation that must be showen ingame?
Player.PlayerGui:WaitForChild("ScreenGui"):Destroy()
AllowReloading = false -- We don't wanna allow reloading when tool is not equipped (Like who reloads a gun that is not in their hand!?!?)
end)
script.Parent.Activated:Connect(function()
if Ammo > 0 and not Reloading then
Ammo = Ammo - 1
NewGui:WaitForChild("TextLabel").Text = Ammo
local MousePos = Mouse.Hit.Position
RemoteEvent:FireServer("Shoot",MousePos)
end
end)
-- A sample function providing one usage of InputBegan
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if AllowReloading == true and input.KeyCode == Enum.KeyCode.R and not Reloading then
Reloading = true
print("R GOT PRESSED!")
RemoteEvent:FireServer("Reload")
NewGui:WaitForChild("TextLabel").Text = "Reloading"
task.wait(2)
Ammo = 10
NewGui:WaitForChild("TextLabel").Text = Ammo
Reloading = false
end
end)
Script:
-- Folder & Services
local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")
local Debris = game:GetService("Debris")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Gun locals
local MaterialFolder = script.Parent:WaitForChild("Materials")
local GunGUI = MaterialFolder.Gui:WaitForChild("ScreenGui")
local Handle = script.Parent:WaitForChild("Handle")
local AlreadyHit = false
local Damage = 10
RemoteEvent.OnServerEvent:Connect(function(Player, Event , MousePos , FinalHit) -- Finalhit can be a bool value to give. To give a instant final blow for example.
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
if Event == "Shoot" then
-- Set the size and other properties of the Bullet part
print(MousePos)
local Bullet = Instance.new("Part")
Bullet.Parent = workspace
Bullet.Size = Vector3.new(0.2, 0.2, 1)
Bullet.CanCollide = false
Bullet.Material = Enum.Material.SmoothPlastic
Bullet.Color = Color3.new(1, 1, 0)
Bullet.Anchored = true
MaterialFolder.Sounds:WaitForChild("(TF2) pistol shot"):Play()
-- Create a new CFrame with the position and orientation of the handle. So bullet is correctly placed
local BulletCF = CFrame.new(Handle.Position, MousePos) * CFrame.Angles(0, math.rad(0), 0)
-- Set the CFrame property of the Bullet part
Bullet.CFrame = BulletCF
-- Set the Bullet part to be removed after a short time
Debris:AddItem(Bullet, 5)
end
if Event == "Equiped" then
-- copy GunGUI and give it to the player (Can be done local but I though maybe using some server communication wouldn't hurt.)
-- Tho performace would be lowered doing so. But better this then exploiters altering GUI
end
if Event == "Deequipped" then
-- copy GunGUI and give it to the player (Can be done local but I though maybe using some server communication wouldn't hurt.)
-- Tho performace would be lowered doing so. But better this then exploiters altering GUI
end
if Event == "Reload" then
MaterialFolder.Sounds:WaitForChild("Gun Reload"):Play()
end
end)
Localization: