So I'm trying to use the touched event to make so when a meshpart touches another player, they take damage. The touched event doesn't work very good for it because it only seems to activate once, and once it's already touching something, it won't activate again until it's not touching it. Is there a better way I could do this?
Local:
-- Player Related
local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
local db = false
-- Replicated Storage
local repStorage = game.ReplicatedStorage
local iceEvents = repStorage:WaitForChild("Skills"):WaitForChild("Ice")
local iceEvent = iceEvents:WaitForChild("Ice")
-- Actual Event
mouse.KeyDown:Connect(function(key)
if key == "z" and db == false then
db = true
iceEvent:FireServer(key)
wait(5)
db = false
end
end)
Server:
local repStorage = game:GetService("ReplicatedStorage")
local iceEvents = repStorage:WaitForChild("Skills"):WaitForChild("Ice")
local iceEvent = iceEvents:WaitForChild("Ice")
local ice1 = script:WaitForChild("Ice1")
local ice2 = script:WaitForChild("Ice2")
local waitTime = 0.01
local transparency = 0.2
iceEvent.OnServerEvent:Connect(function(player,key)
if key == "z" then
local char = game.Workspace:WaitForChild(player.Name)
local rootPart = char:WaitForChild("HumanoidRootPart")
-- Making Ice Variables
local ice11 = ice1:Clone()
local ice21 = ice2:Clone()
local ice12 = ice1:Clone()
local ice22 = ice2:Clone()
local ice13 = ice1:Clone()
local ice23 = ice2:Clone()
local ice14 = ice1:Clone()
local ice24 = ice2:Clone()
local ice15 = ice1:Clone()
local ice25 = ice2:Clone()
local ice16 = ice1:Clone()
local ice26 = ice2:Clone()
-- Settings Parents of All Ices
ice11.Parent = game.Workspace
ice21.Parent = game.Workspace
ice12.Parent = game.Workspace
ice22.Parent = game.Workspace
ice13.Parent = game.Workspace
ice23.Parent = game.Workspace
ice14.Parent = game.Workspace
ice24.Parent = game.Workspace
ice15.Parent = game.Workspace
ice25.Parent = game.Workspace
ice16.Parent = game.Workspace
ice26.Parent = game.Workspace
-- Setting Transparencies
ice11.Transparency = transparency
ice21.Transparency = transparency
ice12.Transparency = transparency
ice22.Transparency = transparency
ice13.Transparency = transparency
ice23.Transparency = transparency
ice14.Transparency = transparency
ice24.Transparency = transparency
ice15.Transparency = transparency
ice25.Transparency = transparency
ice16.Transparency = transparency
ice26.Transparency = transparency
-- Setting Positions of All Ices
ice11.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*15
wait(waitTime)
ice21.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*20
wait(waitTime)
ice12.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*30
ice12.Size = Vector3.new(25,25,25)
wait(waitTime)
ice22.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*40
ice22.Size = Vector3.new(30,30,30)
wait(waitTime)
ice13.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*50
ice13.Size = Vector3.new(35,35,35)
wait(waitTime)
ice23.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*60
ice23.Size = Vector3.new(40,40,40)
wait(waitTime)
ice14.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*70
ice14.Size = Vector3.new(45,45,45)
wait(waitTime)
ice24.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*80
ice24.Size = Vector3.new(55,55,55)
wait(waitTime)
ice15.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*90
ice15.Size = Vector3.new(65,65,65)
wait(waitTime)
ice25.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*100
ice25.Size = Vector3.new(90,90,90)
wait(waitTime)
ice16.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*110
ice16.Size = Vector3.new(115,115,115)
wait(waitTime)
ice26.CFrame = rootPart.CFrame + rootPart.CFrame.lookVector*120
ice26.Size = Vector3.new(120,120,120)
wait(waitTime)
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name == "Ice1" or v.Name == "Ice2" and v.ClassName == "MeshPart" then
v.Touched:Connect(function(hit) -- Touched Event
if hit.ClassName == "Union" and hit.Parent.Name ~= player.Name then
hit.Parent.Humanoid:TakeDamage(math.random(10,20))
end
end)
end
end
wait(3)
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name == "Ice1" or v.Name == "Ice2" and v.ClassName == "MeshPart" then
v:Destroy()
end
end
end
end)
Sorry if the server script is excessively long, but I hope I made it organized enough that it doesn't matter too much.

13 comments
Try creating a RemoteEvent
I did, that's why there's multiple scripts . . .
The problem isn't FE based, I've had this problem before. It did the same thing without FE when I wasn't using RemoteEvents.
I feel like this script could be shortened.
^ U simply cant.
So there's no alternative to Touched? Is there not a way I could make this actually work? I've seen plenty of other games with stuff like this that works just fine.
And yeah, it probably could be shortened e.e
Either change the logic of your code or your game. Your code should not look like that.
Also, why is the server handling user input?
Additionally, KeyDown is deprecated, use UserInputService and ContextActionService instead.
For one, the KeyDown is in the local script, not the server, and with UserInputService, I would use that, but I don't know how to make so it doesn't activate when chat is open. Also, this wasn't the original question, the question was is there another way I could do this so that the touched event works better.
Also, what do you mean my code "should not look like that". If you're going to criticize my work, at least be more detailed so I can properly learn from my mistakes.
Also, what makes KeyDown "deprecated" in the first place?