Script not working

Hey devforum, this most may be a long one as it answers some questions that I have regarding my system that I am making.

I am currently making a spell system that allows you to type a spell in chat and when you click on a player, the spell effects will appear on the character, although I’ve came into a minor issue when doing this.


  1. Would it be easier for me to create module scripts for each spell, instead of a big module / script?
  2. Retrieving a click from a player, I have made a system although I believe it’s not efficient for this use.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")


-- KEY BINDS -- 

uis.InputBegan:Connect(function(key,IsTyping)
	if not IsTyping then
		if key.KeyCode == Enum.KeyCode.G then -- PAIN INFLICTION
			local Model = mouse.Target:FindFirstAncestorOfClass("Model")
			if Model then
				local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
				game.ReplicatedStorage.Events.SpellEvent.KeybindEvent:FireServer(player,ThePlayer,"pain")
			end
		end
	end
end)

uis.InputBegan:Connect(function(key,IsTyping)
	if not IsTyping then
		if key.KeyCode == Enum.KeyCode.C then -- PUSH PLAYER
			local Model = mouse.Target:FindFirstAncestorOfClass("Model")
			if Model then
				local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
				game.ReplicatedStorage.Events.SpellEvent.KeybindEvent:FireServer(player,ThePlayer,"push")
			end
		end
	end
end)

uis.InputBegan:Connect(function(key,IsTyping)
	if not IsTyping then
		if key.KeyCode == Enum.KeyCode.K then -- BREAK BONES
			local Model = mouse.Target:FindFirstAncestorOfClass("Model")
			if Model then
				local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
				game.ReplicatedStorage.Events.SpellEvent.KeybindEvent:FireServer(player,ThePlayer,"breakbone")
			end
		end
	end
end)

-- SPELLS -- 

local Spells = {"Incendia","Motus","incendia","motus","ossox","Ossox","Phasmatos morsinus pyrox allum","phasmatos morsinus pyrox allum","Phasmatos incendia circulum","phasmatos incenda circulum"}

uis.InputBegan:Connect(function(key,IsTyping)
	if not IsTyping then
		if game.ReplicatedStorage.Events.SpellEvent.SpellName.Value ~= "" or nil then
			if key.UserInputType == Enum.UserInputType.MouseButton1 then
				local Model = mouse.Target:FindFirstAncestorOfClass("Model")
				if Model then
					local ThePlayer = game.Players:GetPlayerFromCharacter(Model)
					if game.ReplicatedStorage.Events.SpellEvent.SpellName.Value == Spells then
						game.ReplicatedStorage.Events.SpellEvent:FireServer(player,ThePlayer,game.ReplicatedStorage.Events.SpellEvent.SpellName.Value)
					end
				end
			end
		end
	end
end)

local camera = game.Workspace.CurrentCamera


game.ReplicatedStorage.Events.RagdollMain.OnClientEvent:Connect(function(Type, target)
	if Type == "Ragdoll" then
		local targetcharacter = game.Workspace:FindFirstChild(target)
		local humanoid = targetcharacter.Humanoid
		camera.CameraSubject = targetcharacter.Head
		humanoid:ChangeState("Physics")
	elseif Type == "Unragdoll" then
		local targetcharacter = game.Workspace:FindFirstChild(target)
		local humanoid = targetcharacter.Humanoid
		camera.CameraSubject = targetcharacter.Humanoid
		humanoid:ChangeState("Ragdoll")
	end
end)

As seen above, that is a client-sided script that controls if a player clicks on the other player, is there a way for me to improve this at all, and as for that system above does not work with my current system. (Not actually performing the spell.)

Server

local painModule = require(game.ReplicatedStorage.SpellModules.PainInfliction)
local Spells = {"Incendia","Motus","incendia","motus","ossox","Ossox","Phasmatos morsinus pyrox allum","phasmatos morsinus pyrox allum","Phasmatos incendia circulum","phasmatos incenda circulum"}

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Character = Instance.new("StringValue",leaderstats)
	Character.Name = "Character"
	
	player.Chatted:Connect(function(message)
		for _, spell in pairs(Spells) do
			if message == spell then
				game.ReplicatedStorage.Events.SpellEvent.SpellName.Value = message
				print(message)
			end
		end
	end)
end)

game.ReplicatedStorage.Events.SpellEvent.OnServerEvent:Connect(function(caster, target, spell)
	
	if spell == "Motus" or spell == "motus" then
		local RagdollModule = require(game.ReplicatedStorage.Ragdoll)
		local TargetCharacter = game.Players:FindFirstChild(target).Character
		local Humanoid = TargetCharacter.Humanoid
		
		if Humanoid then
			RagdollModule:Ragdoll(game.Players:FindFirstChild(target),TargetCharacter)
			wait(2)
			RagdollModule:Ragdoll(game.Players:FindFirstChild(target),TargetCharacter,Humanoid)
		end
		
	end
	
	
	if spell == "Phasmatos morsinus pyrox allum" or spell == "Pain" then
		local damage = 0
			damage = 2
			print(damage)

			local TargetCharacter = game.Workspace:FindFirstChild(target)
			local Humanoid = target.Character.Humanoid
			local Animation = game.ReplicatedStorage:WaitForChild("GameCore"):WaitForChild("Witches"):WaitForChild("Animations").MassPain

			if TargetCharacter and Humanoid then
				Humanoid:LoadAnimation(Animation)
				caster.Character.Humanoid.Humanoid:LoadAnimation(game.ReplicatedStorage.GameCore.Witches.Animations.PoenaDolorisCasted)
				repeat wait(1)
					Humanoid:TakeDamage(damage)

					if Animation.IsCompleted then
						break
					end

				until Animation.IsCompleted
			end

		end
	
end)

And, how would I go about scripting the systems, I have a rough idea but I’m currently stuck, any ideas & suggestions will be counted and appreciated.

Thank you.

Here’s one way you can get the mouse instance from the player object (and record the position of the mouse whenever a click action is performed):

local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	local mousePos = mouse.Hit.p
end)

Here’s another way with the UserInputService:

local uis = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()

uis.InputBegan:Connect(function(input, proc)
	if proc then --if coregui processed input ignore the input
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then --left mouse button clicked
		local mousePos = uis:GetMouseLocation()
	end
end)

Hi - sorry for the late reply but having some issues with that script.

SCRIPTS:

Server:

local painModule = require(game.ReplicatedStorage.SpellModules.PainInfliction)
local Spells = {"Incendia","Motus","incendia","motus","ossox","Ossox","Phasmatos morsinus pyrox allum","phasmatos morsinus pyrox allum","Phasmatos incendia circulum","phasmatos incenda circulum"}

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Character = Instance.new("StringValue",leaderstats)
	Character.Name = "Character"
	
	player.Chatted:Connect(function(message)
		for _, spell in pairs(Spells) do
			if message == spell then
				game.ReplicatedStorage.Events.SpellEvent.SpellName.Value = message
				print(message)
			end
		end
	end)
end)

game.ReplicatedStorage.Events.SpellEvent.OnServerEvent:Connect(function(caster, target, spell)
	
	if spell == "Motus" or spell == "motus" then
		print(spell)
		print(caster.Name)
		print(target)
		local MotusModule = require(game.ReplicatedStorage.SpellModules.Motus)
		local targetplr = game.Players:FindFirstChild(target)
		local TargetCharacter = game.Workspace:FindFirstChild(targetplr)
		local Humanoid = TargetCharacter.Humanoid
		
		if Humanoid then
			MotusModule:Motus(targetplr)
		end
		
	end
	
	
	if spell == "Phasmatos morsinus pyrox allum" or spell == "Pain" then
		local damage = 0
			damage = 2
			print(damage)

			local TargetCharacter = game.Workspace:FindFirstChild(target)
			local Humanoid = target.Character.Humanoid
			local Animation = game.ReplicatedStorage:WaitForChild("GameCore"):WaitForChild("Witches"):WaitForChild("Animations").MassPain

			if TargetCharacter and Humanoid then
				Humanoid:LoadAnimation(Animation)
				caster.Character.Humanoid.Humanoid:LoadAnimation(game.ReplicatedStorage.GameCore.Witches.Animations.PoenaDolorisCasted)
				repeat wait(1)
					Humanoid:TakeDamage(damage)

					if Animation.IsCompleted then
						break
					end

				until Animation.IsCompleted
			end

		end
	
end)

Module:

local M = {}


function M:Motus(target)
	local RagdollModule = require(game.ReplicatedStorage.Ragdoll)
	local TargetHumanoid = target.Character.Humanoid
	local TargetCharacter = game.Workspace:FindFirstChild(target)
	
	RagdollModule:Ragdoll(target,TargetCharacter)
	
	game.ReplicatedStorage.GameCore.Witches.Sounds.SpellCasted2:Play()
	
	local PushForce = Instance.new("BodyVelocity")
	PushForce.Name = "PushForce"
	PushForce.MaxForce = Vector3.new(20,20,20)
	PushForce.Velocity = {-TargetCharacter.HumanoidRootPart.CFrame.loovVector} * 25
	PushForce.Parent = TargetCharacter.HumanoidRootPart
	
	wait(0.25)
	PushForce:Destroy()
	
	
	wait(0.50)
	RagdollModule:UnRagdoll(target,TargetCharacter,TargetHumanoid)
end


return M

Client:

local uis = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()

local mouse = player:GetMouse()

-- SPELLS --

uis.InputBegan:Connect(function(input, proc)
	if proc then --if coregui processed input ignore the input
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then --left mouse button clicked
		local Model = mouse.Target:FindFirstAncestorOfClass("Model")
		if Model then
			local TargetPlayer = game.Players:GetPlayerFromCharacter(Model)
			if game.ReplicatedStorage.Events.SpellEvent.SpellName.Value ~= "" or nil then
				local spell = game.ReplicatedStorage.Events.SpellEvent.SpellName.Value
				game.ReplicatedStorage.Events.SpellEvent:FireServer(TargetPlayer,spell)
			end
		end
	end
end)


local camera = game.Workspace.CurrentCamera


game.ReplicatedStorage.Events.RagdollMain.OnClientEvent:Connect(function(Type, target)
	if Type == "Ragdoll" then
		local targetcharacter = game.Workspace:FindFirstChild(target)
		local humanoid = targetcharacter.Humanoid
		camera.CameraSubject = targetcharacter.Head
		humanoid:ChangeState("Physics")
	elseif Type == "Unragdoll" then
		local targetcharacter = game.Workspace:FindFirstChild(target)
		local humanoid = targetcharacter.Humanoid
		camera.CameraSubject = targetcharacter.Humanoid
		humanoid:ChangeState("Ragdoll")
	end
end)

Those errors don’t even seem relevant to the code I posted.

I know, just in general I am having some issues with my code that I have contacted 4+ scripters about and I’ve had no luck.

If you cannot help, then I’ll find someone else - apologies if that sounded rude. Currently quite stressed

Which line is line 30? I can’t see because posting code to the site sometimes has weird line breaks.

Line 30 =

local targetplr = game.Players:FindFirstChild(target)
local TargetCharacter = game.Workspace:FindFirstChild(targetplr) -- line 30

In fact the error mentions that the argument is missing or nil which means “targetplr” is a reference to a nil value.

Ignore this post, snipping it.

It fixed it, somehow?
(30 ariana grandes)

You used targetplr.Name instead?

Yup, and now the only issue is getting the character, which is an easy fix.
image

Yeah, so FindFirstChild requires a string value argument of the instance being searched for, before you were attempting to pass the player object itself as an argument to it.

That error message was a little elusive, considering it mentioned the argument was missing or nil when it wasn’t.

1 Like

Yeah, just do targetplr.Character to get the target character.

I’m surprised that you contacted 4+ scripters & the issue wasn’t found considering how simple it was. Any way best of luck with your project.

1 Like