Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Script doesn't work correctly when I change Parent of part?

Asked by 6 years ago
Edited 6 years ago

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?

1
Ah it's because you're only setting the parent locally User#19524 175 — 6y
1
^ Which I edited my answer for MythicalShade 420 — 6y

2 answers

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

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)

0
:( not what I wanted RetroGalacticGamer 331 — 6y
1
What do you mean? Could you restate your problem because I gave the two solutions to the ways I interpreted your issue. MythicalShade 420 — 6y
0
now look RetroGalacticGamer 331 — 6y
0
I know the problem let me edit my answer MythicalShade 420 — 6y
View all comments (4 more)
0
I will try it RetroGalacticGamer 331 — 6y
0
I don't think that worjed RetroGalacticGamer 331 — 6y
0
I didn't work in game RetroGalacticGamer 331 — 6y
0
Hmm any errors? I'm on my phone so I can't exactly test it... but i'll think about it for a minute MythicalShade 420 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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)
0
That's a good note that :TakeDamage() is much more simple, but that's not the problem he's having. His problem is parenting the part locally which doesn't replicate to the server due to FE. MythicalShade 420 — 6y
0
ik User#23365 30 — 6y
0
i was giving him another way instead of doing the way of what he was doing User#23365 30 — 6y
0
Oh sorry I was just making sure, good work MythicalShade 420 — 6y
0
ok User#23365 30 — 6y

Answer this question