Hi everyone. I am trying to script a dart gun for a game I'm working on. The gun uses some simple remote events to communicate client to server.
The local script tests to see if the tool is equipped and is supposed to fire server. The server reads this and creates the dart projectile and mesh.
Then the local script sends another signal when the tool is clicked to clone the mesh.
Local script below: IGNORE VARIABLES
--Constraints local IgnoreList = {rocket = 0, handle = 1, effect = 1, water = 1} -- Rocket will fly through things named these --NOTE: Keys must be lowercase, values must evaluate to true --Variables local tool = script.Parent local player = game.GetService("Players").LocalPlayer local mouse = player:GetMouse() -----InTool local configs = game.ReplicatedStorage.MaverickConfigurations local gungui = tool:WaitForChild("GunGUI" ) local firesound = tool.WaitForChild("FireSound") local reloadsound = tool.WaitForChild("ReloadSound") local primesound = tool.WaitForChild("PrimeSound") local reloading = false --reloading check local priming = false -- priming check local capacity = configs.Capacity.Value local gravityacceleration = 196.2 local dartmeshid = 'rbxassetid://2906043627' local meshscale = Vector3.new(0.17,0.17,.25) local dartpartscale = Vector3.new(1, 1, 1) local dartcolor = BrickColor.new(33, 60, 122) local spawnPosition = tool.Model.Muzzle.CFrame() --local contextActionService = game:GetService("ContextActionService") --caters to mobile gamers (A Later Update) local bodytype = nil local difference = 0 -- dif between head and mouse local replicatedstorage = game:GetService("ReplicatedStorage") local gungui = tool:WaitForChild("GunGUI") --remote events local equipAnimation = replicatedstorage:WaitForChild("EquipAnimation") local unequipanimation = replicatedstorage:WaitForChild("UnequipAnimation") local primeAnimation = replicatedstorage:WaitForChild("PrimeAnimation") local shootevent = replicatedstorage:WaitForChild("ShootEvent") local prime = replicatedstorage:WaitForChild("Prime") local reload = replicatedstorage:WaitForChild("Reload") local equip = replicatedstorage:WaitForChild("Equip") local headshot = replicatedstorage:WaitForChild("Headshot") --remote functions local checkBodyType = replicatedstorage:WaitForChild("CheckBodyType") local fetchRemainingAmmo = replicatedstorage:WaitForChild("FetchRemainingAmmo") --When tool is equipped this script will run tool.Equipped:Connect(function(mouse) replicatedstorage.equip:FireServer(mouse) gungui:Clone().Parent = player.PlayerGui -- cloning gui into player on equip equipAnimation:FireServer(tool.shoot) -- calling remote event to add to server equip animation mouse.Icon = "rbxassetid://3862353028" end) local function OnActivated() --shoot! if reloading == true or priming == true then -- else replicatedstorage.ShootEvent:FireServer(spawnPosition,tool) end end
Server Script:
--ServerScript ----------------- --| Constants | ----------------- local player = game.Players.LocalPlayer local replicated = game:GetService("ReplicatedStorage") local configs = replicated:WaitForChild("MaverickConfigurations") local coolDown = configs.AttackCooldown.Value local Gravity = 196.2 local reloadtime = configs.ReloadTime.Value -- Seconds until tool can be used again local dartspeed = configs.DartSpeed.Value -- Speed of the projectile local dartmesh = 'http://www.roblox.com/asset/?id=2251534' local meshscale = Vector3.new(0.35, 0.35, 0.25) local dartsize = Vector3.new(1.2, 1.2, 3.27) game.ReplicatedStorage.MaverickConfigurations.Equip.OnServerEvent:Connect(function(player, mouse) --NOTE: We create the dart once and then clone it when the player fires local Dart = Instance.new('Part') do -- Set up the dart part Dart.Name = 'Dart' Dart.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size Dart.Size = dartsize Dart.CanCollide = false -- Add the mesh local mesh = Instance.new('SpecialMesh', Dart) mesh.MeshId = dartmesh mesh.Scale = meshscale -- Add a force to counteract gravity local bodyForce = Instance.new('BodyForce', Dart) bodyForce.Name = 'Antigravity' bodyForce.force = Vector3.new(0, Dart:GetMass() * Gravity, 0) -- Finally, clone the dart script and enable it -- local dartScriptClone = DartScript:Clone() -- dartScriptClone.Parent = Dart -- dartScriptClone.Disabled = false end game.ReplicatedStorage.MaverickConfigurations.ShootEvent.OnServerEvent:Connect(function(player,spawnPosition,tool) -- Create a clone of dart and set its color local dartClone = Dart:Clone() --game.Debris:AddItem(dartClone, 30) dartClone.BrickColor = BrickColor.new(33, 60, 122) -- Position the dart clone and launch! dartClone.CFrame = CFrame.new(spawnPosition, target) --NOTE: This must be done before assigning Parent dartClone.Velocity = dartClone.CFrame.lookVector * dart_SPEED --NOTE: This should be done before assigning Parent dartClone.Parent = game.Workspace delay(coolDown, function() end) spawn(function() wait(30) if dartClone then dartClone:Destroy() end end) end
Thanks for your help everyone!
You can't create meshes through scripts due to Roblox security. The best method is to create it and put it in Replicated Storage and clone it.