I’m making a spell system. And there’s a script where I need to summon a spike where the mouse is.
Some things to know:
- This is a ModuleScript inside a Script,
- The player can be in first person or 3rd person at will,
- I have set the PrimaryPart to the spike,
- I use plr:GetMouse and mouse.Hit.Position,
- plr is got with the RemoteEvent from the second LocalScript
- The error message is “attempt to index nil with ‘Hit’”
Here’s the ModuleScript for executing the spell,
elseif act == "P1" then
local mouse = plr:GetMouse()
local target = mouse.Hit.Position
local sp = ReplicatedStorage:WaitForChild("Spayk"):Clone()
sp.Parent = workspace
sp:MoveTo(target)
local alreadyHit = {}
local destroyScheduled = false
local function damg(hit)
local parent = hit.Parent
if not parent and alreadyHit[parent] then return end
alreadyHit[parent] = true
local humanoid = parent:FindFirstChild("Humanoid")
local targetplayer = Players:GetPlayerFromCharacter(parent)
if humanoid then
if isPlayerCharacter(parent) then
if canDamage(plr, targetplayer) then
humanoid:TakeDamage(60)
playHitExclamationSound(hit)
end
else
humanoid:TakeDamage(55)
playHitExclamationSound(hit)
end
if not destroyScheduled then
destroyScheduled = true
task.wait(5)
safeDestroy(sp)
end
end
sp.Hurt.Touched:Connect(damg)
task.wait(5)
safeDestroy(sp)
end
Here’s the LocalScript for firing the spell,
if i.KeyCode == spell1key then
if db1 == false then
db1 = true
rep:FireServer("P1")
task.wait(20)
db1 = false
end
end