Questions about running server script code from a local script

Okay, I have a few questions about running code that shows up to everyone in a local script. I have a cannon, but the shooting function is currently in a local script so only the client can see the shells and damage. I will have to run server script code so everyone can see the shells and explosions.
How do I do this?
I tried using a module script, but it didn’t work since module script code ran from a LScript only is client sided.
Would remote events work?
Please tell me how to run server code in a local script.
Can I make global function on a server script?

Remote events would be your best bud here. You can use :FireAllClients() to replicate whatever event you wish to use to all clients in the game.

If it were me I would have two remotes (FireCannon, ReplicateShells) and my psuedo-code would look something like this:

  1. Use my remote to fire the cannon and do whatever server-sided checking is necessary
  2. Use the ReplicateShells remote and FireAllClients so that they all see whatever it is you plan to do

Wait what??? (Sorry im big noob.)
So I should make a remote event, when the player presses c.
then when the event is fired i run a script? However, the function to shoot has a LOT of variables and if i paste it in, i will have 129013120930139012 reference errors.
My shoot code:

function FireMachineGun(GunParent)
	while FiringGun do
		for _,v in pairs(GunParent:GetChildren()) do
			if v:IsA("BasePart") then
				if v.Name == "Cannon" then
					local explosion = Instance.new("Explosion")
					explosion.BlastRadius = 0
					explosion.BlastPressure = 0
					explosion.Position = v.Position
					explosion.Parent = game.Workspace		
					local Part = Instance.new("Part")
					Part.BrickColor = BrickColor.new("Crimson")
					Part.Name = "Bullet"
					Part.Material = Enum.Material.Neon
					Part.CanCollide = false
					Part.FormFactor = "Symmetric"
					Part.Size = Vector3.new(40,40,24)
					Part.BottomSurface = "Smooth"
					Part.TopSurface = "Smooth"
					local Mesh = Instance.new("BlockMesh")
					Mesh.Parent = Part
					Mesh.Scale = Vector3.new(1/25,1/25,1)
					local BV = Instance.new("BodyVelocity")
					BV.Parent = Part
					BV.maxForce = Vector3.new(math.huge,math.huge,math.huge)
					local PlaneTag = Instance.new("ObjectValue")
					PlaneTag.Parent = Part
					PlaneTag.Name = "PlaneTag"
					PlaneTag.Value = Plane
					local e = math.random(1,7)
					local effective = math.random(1,21)
					Part.Touched:connect(function(Object)
						local blueSpaceplane = Object:FindFirstAncestor("Blue Spaceplane")
						local blueInterceptor = Object:FindFirstAncestor("Blue Interceptor")
						if (not Object:IsDescendantOf(Character)) then
							local HitHumanoid = Object.Parent:findFirstChild("Humanoid")
							if HitHumanoid then
								HitHumanoid:TakeDamage(100)
								local CreatorTag = Instance.new("ObjectValue")
								CreatorTag.Name = "creator"
								CreatorTag.Value = Player
								CreatorTag.Parent = HitHumanoid
							else if v.Name == 'Cannon' and (not HitHumanoid) and e == 2 and (blueSpaceplane or blueInterceptor) then
								local explosion = Instance.new("Explosion")
								explosion.BlastRadius = 5
								explosion.BlastPressure = 500
								explosion.Position = Object.Position
								explosion.Parent = game.Workspace
								end
							end
						end
					end)
					Part.Parent = game.Workspace
					Part.CFrame = v.CFrame + v.CFrame.lookVector * 10
					BV.velocity = (v.Velocity) + (Part.CFrame.lookVector * 2000) + (Vector3.new(0,0.15,0))
					delay(3,function() Part:Destroy() end)
				end
			end
		end
		wait(GunTime2)
	end
end

@Syntheix

When it comes to actually making the code I’d be the worst person to ask. I’ve never personally worked with anything gun related although I hope to practice with raycasting and the physics engine soon.

1 Like

Oh, okay. How would I transport the variables from my L-Script to the server script so it can shoot?

Also, can I check when the player has the C key down in a server script?

you just gotta get all the functions for creating the cannon ball part into the server

1 Like

fireallclients is not needed, its just creating a part not an GUI, FireServer is a better choice imo

1 Like

Can I get free robux local player in a server script?

Oops, I forgot that the server replicates and overrides any client replication. I guess you wouldn’t need an extra remote, you could do it all through that singular one while using FireServer

Can I get free robux local player in a server script?

Ha, unfortunately no. What you can do however is get the instance of the player who called the remote, which is an included argument for FireServer.

1 Like

each plane waits until someone goes in, then it calls local player and stores it in a variable. Can i transfer the variable over to the server script?

yeah easily, the first thing you get in an function is the player, for example:

remotefunction.OnServerEvent:Connect(function(plr)
plr --this is the player
end)
1 Like

Although plr is a parameter, and an event will be fired then the cannon fires in the SERVER SCRIPT not the local script, so i doubt function parameters would do much good. ( sorry if im wrong)

ofcourse! thats what remote evenst are for! for example:

--client firing the strings
local a = 1
event:FireServer(a)
--server receiving it
event.OnServerEvent:Connect(function(plr,a)
--plr will always be the first string the server get, then a
end)
1 Like

so that means i can transfer the thing over??? wow! can i ping you tomorrow since i gotta go to sleep. ( im a kid)

1 Like

ofc, remote events are awsome :stuck_out_tongue:

1 Like

theres an lazy way of doing this, fire an event every second updating the cannonball on client side into the server side, making two balls, one for client and one for server

1 Like

sure, just incase i’m not online, heres all you need to know for remote events:
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

1 Like

What I didn’t tell you was that the game was a dogfight game, so the cannons are aircraft cannons

@foxnoobkite, it’s not working.
I have a remote event called Shoot.
Here is the code.
Running code:(This is in a LocalScript)

function FireMachineGun(GunParent)
	local Shoot = script.Parent.Shoot
	while FiringGun do
		Shoot:FireServer(GunParent,FiringGun,Plane,Character,Player)
	end
end

Shooting Code:
(This in a script.(server))

local Shoot = script.Parent.Shoot
Shoot.OnServerEvent:Connect(function(GunParent,FiringGun,Plane,Character,Player,GunTime2)
	for _,v in pairs(GunParent:GetChildren()) do
		if v:IsA("Part") then
			if v.Name == "Cannon" then
				local explosion = Instance.new("Explosion")
				explosion.BlastRadius = 0
				explosion.BlastPressure = 0
				explosion.Position = v.Position
				explosion.Parent = game.Workspace		
				local Part = Instance.new("Part")
				Part.BrickColor = BrickColor.new("Navy blue")
				Part.Name = "Bullet"
				Part.Material = Enum.Material.Neon
				Part.CanCollide = false
				Part.FormFactor = "Symmetric"
				Part.Size = Vector3.new(40,40,24)
				Part.BottomSurface = "Smooth"
				Part.TopSurface = "Smooth"
				local Mesh = Instance.new("BlockMesh")
				Mesh.Parent = Part
				Mesh.Scale = Vector3.new(1/25,1/25,1)
				local BV = Instance.new("BodyVelocity")
				BV.Parent = Part
				BV.maxForce = Vector3.new(math.huge,math.huge,math.huge)
				local PlaneTag = Instance.new("ObjectValue")
				PlaneTag.Parent = Part
				PlaneTag.Name = "PlaneTag"
				PlaneTag.Value = Plane
				local e = math.random(1,7)
				local effective = math.random(1,21)
				Part.Touched:connect(function(Object)
					local enemyInterceptor = Object:FindFirstAncestor("Red Chaser")
					local enemyFighter = Object:FindFirstAncestor("Red Spaceplane")
					local enemyChaser = Object:FindFirstAncestor("Red Interceptor")
					if (not Object:IsDescendantOf(Character)) then
						local HitHumanoid = Object.Parent:findFirstChild("Humanoid")
						if HitHumanoid then
							HitHumanoid:TakeDamage(100)
							local CreatorTag = Instance.new("ObjectValue")
							CreatorTag.Name = "creator"
							CreatorTag.Value = Player
							CreatorTag.Parent = HitHumanoid
						else if v.Name == 'Cannon' and (not HitHumanoid) and e == 2 and (enemyFighter or enemyInterceptor or enemyChaser) then
								local explosion = Instance.new("Explosion")
								explosion.BlastRadius = 5
								explosion.BlastPressure = 500
								explosion.Position = Object.Position
								explosion.Parent = game.Workspace
							end
						end
					end
				end)
				Part.Parent = game.Workspace
				Part.CFrame = v.CFrame + v.CFrame.lookVector * 10
				BV.velocity = (v.Velocity) + (Part.CFrame.lookVector * 2000) + (Vector3.new(0,0.15,0))
				delay(3,function() Part:Destroy() end)
			end
		end
	end
	wait(GunTime2)
end)

When I press c, nothing happens but no errors.