My Dart Gun Script has server-client communication issues. How can I fix this?
I am currently building a Nerf Gun FPS. I am currently struggling with communication between server and client. My goal is to spawn a dart using a server script and remote functions. Script is below:
04 | local serverStorage = game:GetService( "ServerStorage" ) |
05 | local replicatedStorage = game:GetService( "ReplicatedStorage" ) |
06 | local KOValue = "Kills" |
07 | local WOValue = "Wipeouts" |
09 | replicatedStorage.ShootEvent.OnServerEvent:Connect( function (player,tool,position,part) |
10 | if game.Workspace [ player.Name ] .Humanoid.Health < = 0 then |
16 | local dartClone = Dart:Clone() |
18 | dartClone.BrickColor = player.TeamColor |
19 | dartClone.Touched:connect( function (hit) |
20 | if hit and hit.Parent and hit.Parent ~ = player.Character and hit.Parent ~ = tool then |
27 | if dartClone then dartClone:Destroy() end |
31 | local spawnPosition = (tool.Handle.CFrame * CFrame.new( 2 , 0 , 0 )).p |
32 | dartClone.CFrame = CFrame.new(spawnPosition, target) |
33 | dartClone.Velocity = dartClone.CFrame.lookVector * dartspeed |
34 | dartClone.Parent = game.Workspace |
37 | local creatorTag = Instance.new( 'ObjectValue' , dartClone) |
38 | creatorTag.Value = player |
39 | creatorTag.Name = 'creator' |
40 | local iconTag = Instance.new( 'StringValue' , creatorTag) |
41 | iconTag.Value = tool.TextureId |
45 | delay(delay, function () |
50 | if part.Name = = "Head" or part:IsA( "Hat" ) and part.Parent:FindFirstChild( "Humanoid" ).Health > 0 then |
52 | replicatedStorage.Headshot:FireClient(player) |
56 | local humanoid = part.Parent:FindFirstChild( "Humanoid" ) |
58 | humanoid = part.Parent.Parent:FindFirstChild( "Humanoid" ) |
60 | humanoid:TakeDamage(damage) |
61 | if humanoid.Health < = 0 then |
62 | player.leaderstats [ KOValue ] .Value = player.leaderstats [ KOValue ] .Value + 1 |
63 | game.Players [ humanoid.Parent.Name ] .leaderstats [ WOValue ] .Value = game.Players [ humanoid.Parent.Name ] .leaderstats [ WOValue ] .Value + 1 |
74 | replicatedStorage.EquipAnimation.OnServerEvent:Connect( function (player,animation) |
75 | local newAnim = game.Workspace [ player.Name ] .Humanoid:LoadAnimation(animation) |
77 | replicatedStorage.UnequipAnimation.OnServerEvent:Connect( function (player,animation) |
79 | for i,v in pairs (game.Workspace:GetChildren()) do |
80 | if v.Name = = player.Name.. "'s Trajectory" then |
85 | replicatedStorage.Reload.OnServerEvent:Connect( function (player,animation) |
87 | local reloadAnim = game.Workspace [ player.Name ] .Humanoid:LoadAnimation(animation) |
Local Script Below:
002 | local IgnoreList = { rocket = 0 , handle = 1 , effect = 1 , water = 1 } |
005 | local tool = script.Parent |
007 | local player = game.GetService( "Players" ).LocalPlayer |
009 | local mouse = player:GetMouse() |
012 | local gungui = tool:WaitForChild( "GunGUI" ) |
013 | local firesound = tool.WaitForChild( "FireSound" ) |
014 | local reloadsound = tool.WaitForChild( "ReloadSound" ) |
015 | local primesound = tool.WaitForChild( "PrimeSound" ) |
016 | local primedelay = tool.Configurations:WaitForChild( "PrimeDelay" ) |
017 | local damage = tool.Configurations:WaitForChild( "Damage" ) |
018 | local range = tool.Configurations:WaitForChild( "Range" ) |
019 | local dartspeed = tool.Configurations:WaitForChild( "DartSpeed" ) |
020 | local reloadtime = tool.Configurations:WaitForChild( "ReloadTime" ) |
021 | local primingtime = tool.Configurations:WaitForChild( "PrimeTime" ) |
025 | local reloading = false |
029 | local capacity = tool:WaitForChild( "Capacity" ) |
031 | local CreatorTag = Dart:WaitForChild( 'creator' , 60 ) |
033 | local gravityacceleration = 196.2 |
037 | local meshscale = Vector 3. new( 0.17 , 0.17 ,. 25 ) |
039 | local dartpartscale = Vector 3. new( 1 , 1 , 1 ) |
041 | local dartcolor = BrickColor.new( 33 , 60 , 122 ) |
043 | local dartscript = script:WaitForChild( 'Dart' ) |
052 | local replicatedstorage = game.GetService( "ReplicatedStorag" ) |
054 | local gungui = tool:WaitForChild( "GunGUI" ) |
057 | local equipAnimation = replicatedstorage:WaitForChild( "EquipAnimation" ) |
059 | local unequipanimation = replicatedstorage:WaitForChild( "UnequipAnimation" ) |
061 | local primeAnimation = replicatedstorage:WaitForChild( "PrimeAnimation" ) |
063 | local shootevent = replicatedstorage:WaitForChild( "ShootEvent" ) |
065 | local prime = replicatedstorage:WaitForChild( "Prime" ) |
067 | local reload = replicatedstorage:WaitForChild( "Reload" ) |
069 | local headshot = replicatedstorage:WaitForChild( "Headshot" ) |
071 | local checkBodyType = replicatedstorage:WaitForChild( "CheckBodyType" ) |
073 | local fetchRemainingAmmo = replicatedstorage:WaitForChild( "FetchRemainingAmmo" ) |
075 | function findBodyType() |
076 | bodytype = checkBodyType:InvokeServe(tool) |
082 | local function OnTouched(otherPart) |
083 | if Dart and otherPart then |
085 | if IgnoreList [ string.lower(otherPart.Name) ] then |
090 | tool.Equipped:Connect( function (mouse) |
091 | gungui:Clone().Parent = player.PlayerGui |
093 | equipAnimation:FireServer(tool.shoot) |
096 | mouse.Button 1 Down:Connect( function () |
097 | if Capacity < = 0 or reloading = = true or priming = = true or cooldown = = true then |
100 | local head = game.Workspace [ player.Name ] .Head.CFrame.lookVector |
101 | local mouse = CFrame.new(game.Workspace [ player.Name ] .Head.Position,mouse.Hit.p).lookVector |
102 | difference = (head-mouse) |
106 | local function OnActivated() |
107 | local myModel = player.Character |
108 | if Tool.Enabled and myModel and myModel:FindFirstChild( 'Humanoid' ) and myModel.Humanoid.Health > 0 then |
110 | game.ReplicatedStorage.ROBLOX_RocketFireEvent:FireServer(mouse.Hit.p) |
114 | if humanoid and humanoid.Health > 0 then |
115 | if hitPart.Name = = 'Torso' then |
117 | humanoid:TakeDamage( 100 ) |
123 | local function OnTouched(otherPart) |
124 | if Dart and otherPart then |
126 | if IgnoreList [ string.lower(otherPart.Name) ] then |
130 | local myPlayer = CreatorTag.Value |
133 | if myPlayer.Character and myPlayer.Character:IsAncestorOf(otherPart) then |
138 | if not myPlayer.Neutral then |
139 | local character = FindCharacterAncestor(otherPart.Parent) |
140 | local player = PlayersService:GetPlayerFromCharacter(character) |
141 | if player and player ~ = myPlayer and player.TeamColor = = Dart.BrickColor then |
148 | if otherPart = = game.Workspace.Terrain then |
150 | local frontOfRocket = Dart.Position + (Dart.CFrame.lookVector * (Dart.Size.Z / 2 )) |
151 | local cellLocation = game.Workspace.Terrain:WorldToCellPreferSolid(frontOfRocket) |
152 | local cellMaterial = game.Workspace.Terrain:GetCell(cellLocation.X, cellLocation.Y, cellLocation.Z) |
153 | if cellMaterial = = Enum.CellMaterial.Water or cellMaterial = = Enum.CellMaterial.Empty then |
159 | explosion.Hit:connect( function (hitPart, hitDistance) OnExplosionHit(hitPart, hitDistance, explosion.Position) end ) |
162 | CreatorTag.Parent = script |
168 | Dart.Touched:connect(OnTouched) |
169 | if difference.magnitude < 1.33 then |
170 | shootevent:FireServer(tool,position,part) |
171 | Capacity.Value = Capacity.Value - 1 |