How can I improve my code for future spells/abilities

local:

local UIS = game:GetService("UserInputService")
local char = script.Parent
local RE = game.ReplicatedStorage.Revive

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.G then
		RE:FireServer(char)
		print("Used revive ability")
	end
end)

server:

local RE = game.ReplicatedStorage.Revive


RE.OnServerEvent:Connect(function(player, char)
	local sphere = Instance.new("Part")
	sphere.Parent = workspace
	sphere.Shape = Enum.PartType.Ball
	sphere.Position = char.PrimaryPart.Position
	sphere.Size = Vector3.new(10, 10, 10)
	sphere.CanCollide = false
	sphere.Anchored = true
	sphere.Transparency = 0.8

	for i=1,10 do
		wait(0.1)
		sphere.Size = sphere.Size + Vector3.new(2, 0, 2)
		sphere.Touched:Connect(function(part)
			local downed = part.Parent:FindFirstChild("Downed")
			local hum = part.Parent:FindFirstChild("Humanoid")
			if downed and downed.Value == true then
				downed.Value = false
				hum.MaxHealth = 100
				hum.Health = 100
				print("Found downed")
			end
		end)
	end
	sphere:Destroy()
end)

So currentl I have script for revive spell. How can I improve this code, so I can add more spells in one code, not creating new Remote Events? (if it is possible)

You can use a single remote event to handle multiple spells.

In local script, you can fire a string that represents the spell:

RE:FireServer("Revive", char)

and in the server script, you can define spells in different functions, and create a single function for handling the remote event:

local function reviveSpell(player, char)
	-- spell code here
end
-- define other spell functions

-- Function that handles remote events
local function onServerEvent(player, spellName, ...)
	if spellName == "Revive" then
		reviveSpell(player, ...)
	elseif spellName == "Leap" then
		-- call the spell function for leap
	end
end
RE.OnServerEvent:Connect(onServerEvent)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.