I have got a serious problem that I have been trying to solve for a long time. See, I have a part that has a damage script under it. Now I know the damage script will work, because if I don't change the parent, it does. But the goal here is to use a script to change the parent of the part with the damage script applied to it, and have the damage script still working at the end. Here is my damage script, which I know works.
local Damagepack = script.Parent --This script is a ServerSide script local damageAmount = -25 --I also have FilteringEnabled on Damagepack.Touched:Connect(function(otherPart) local Character = otherPart.Parent local Humanoid = Character:FindFirstChild("Humanoid") if Humanoid then local currentHealth = Humanoid.Health local newHealth = currentHealth + damageAmount Humanoid.Health = newHealth end end)
I used a simple local script to change the Parent:
local Tool = script.Parent Tool.Activated:Connect(function() local Example = script.Parent.Part Example.Parent = game.Workspace Tool:Destroy() end)
Here is how everything is arranged:
Tool (Tool) LocalScript (The parent - changer) Part (Part that has damage script applied to it) DamageScript (Script that does damage)
Can you help me?
Since FilteringEnabled is on (enabled by default now), changing the parent of the part in a LocalScript will not work since the change won't replicate to the server. For this, you need to change the parent from a server script, and if you want to do that while still using the tool you need a remoteEvent.
Server Script (Event Script)
local event = Instance.new('RemoteEvent',script.Parent) event.Name = "ChangeParentEvent" -- create a remote event event.OnServerEvent:Connect(function(plr,object,parent) -- Event for when the remoteevent is fired object.Parent = parent -- Set the part's parent to workspace object.Parent:Destroy() -- Destroy tool end)
Server Script (Damage Script inside Part)
local Damagepack = script.Parent local damageAmount = -25 Damagepack.Touched:Connect(function(otherPart) local Character = otherPart.Parent local Humanoid = Character:FindFirstChild("Humanoid") if Humanoid then local currentHealth = Humanoid.Health local newHealth = currentHealth + damageAmount Humanoid.Health = newHealth end end)
Local Script (Script inside of tool)
local Tool = script.Parent local event = Tool:WaitForChild('ChangeParentEvent') Tool.Activated:Connect(function() local Example = script.Parent.Handle event:FireServer(Example,workspace) Tool:Destroy() end)
Object Order : oof
If this doesn't work, say something because I haven't tested it. (Sorry for taking long I was busy)
Instead of using that stuff that your using to make the player take damage for the damage script, you can just use :TakeDamage()
instead which would be more short like this:
local Damagepack = script.Parent Damagepack.Touched:Connect(function(otherPart) if otherPart.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid:TakeDamage(25) -- to change the damage just change the number inside the arguments end end)
this wouldnt make the player take damage if they had a forcefield, but if you wanted the player to take damage, even if they had a forcefield you could do this:
local Damagepack = script.Parent local damageAmount = 25 Damagepack.Touched:Connect(function(otherPart) if otherPart.Parent:FindFirstChild("Humanoid") then hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - damageAmount end end)