Module scripts how to

Hello everyone! I’m trying to implement remote events and module scripts for projectile rendering and hit detection.

So far I’ve managed to implement a throwing weapon that moves and behaves almost exactly as I want it to. However, the system I got running is pretty primitive and easily exploitable because of being handled by a single on server event.
I understand that velocity and trajectory should probably go to a module script, but where is best to put it and how to connect it with server and client without overcomplicating the script?

So, my questions are: how to use remote events and module scripts in this one? How to wrap damage and rendering into modules and where to put them - server or replicated?

-- Tool controller
local Player = game:GetService("Players").LocalPlayer
local mouse = Player:GetMouse()

local tool = script.Parent
local trigger = false

tool.Equipped:Connect(function()
	tool.Handle.Equip:Play()
end)

tool.Activated:Connect(function()
	if trigger == false then
		trigger = true
		tool.WeaponFired:FireServer(mouse.Hit.Position)
		wait(1)
		trigger = false
	end
	
end)

-- Server Script
local Debris = game:GetService("Debris")
local rep = game:GetService('ReplicatedStorage')
t = 0

script.Parent.WeaponFired.OnServerEvent:Connect(function(player, mousepos)
	local star = rep.StarHitbox
	local ch = player.Character
	local hum = ch.Humanoid
	local hrp = ch:WaitForChild('HumanoidRootPart')
	local righthand = ch:WaitForChild('RightHand')
	local tool = ch:FindFirstChild('Star')
	
	local starclone = star:Clone()
	starclone.CanCollide = true
	starclone.Parent = workspace
	starclone:SetNetworkOwner(player)
	starclone.Orientation = Vector3.new(0, math.rad(-90), 0)
	local distance = (mousepos - hrp.Position).magnitude
	local x = print(distance)

  ---  calculate time, trajectory and velocity here and throw the projectile
	
	Debris:AddItem(starclone, t+0.5)

	local ontouch = starclone.Touched:Connect(function(hit)
		local enemy = hit.Parent:FindFirstChild('Humanoid')
		if enemy == hum then return end
		if enemy and enemy ~= hum then
			enemy:TakeDamage(10)
		end
	end)
end)
1 Like

You don’t need to use a RemoteEvent to get the player’s mouse, Tool.Equipped already has a mouse parameter.

Delete the local script and WeaponFired and change the server script

local tool = script.Parent
local trigger = false

local player: Player?
local mouse: Mouse?

local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local rep = game:GetService('ReplicatedStorage')
t = 0

tool.Equipped:Connect(function(_mouse)
	mouse = _mouse
	player = Players:GetPlayerFromCharacter(tool.Parent) -- gets the player of the tool holder
end)

tool.Activated:Connect(function()
	if not trigger and mouse and player then
		trigger = true
		local star = rep.StarHitbox
		local ch = player.Character
		local hum = ch.Humanoid
		local hrp = ch:WaitForChild('HumanoidRootPart')
		local righthand = ch:WaitForChild('RightHand')
		local tool = ch:FindFirstChild('Star')
		
		local starclone = star:Clone()
		starclone.CanCollide = true
		starclone.Parent = workspace
		starclone:SetNetworkOwner(player)
		starclone.Orientation = Vector3.new(0, math.rad(-90), 0)
		local distance = (mousepos - hrp.Position).magnitude
		local x = print(distance)
		
		---  calculate time, trajectory and velocity here and throw the projectile
		
		Debris:AddItem(starclone, t+0.5)
		
		local ontouch = starclone.Touched:Connect(function(hit)
			local enemy = hit.Parent:FindFirstChild('Humanoid')
			if enemy == hum then return end
				
if enemy and enemy ~= hum then
				enemy:TakeDamage(10)
			end
		end)
		task.wait(1)
		trigger = false
	end
end)

tool.Unequipped:Connect(function()
	mouse = nil
	player = nil
end)

I believe that rendering and damage should be done outside the server event, no? How to frame them into modules and where to put those modules is kinda what I’m asking:)

yeah but you’re also saying that your script is easily exploitable because hackers can fire the remote event in their exploit programs

1 Like