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.

01local Damagepack = script.Parent          --This script is a ServerSide script
02local damageAmount = -25                     --I also have FilteringEnabled on
03Damagepack.Touched:Connect(function(otherPart)
04    local Character = otherPart.Parent
05    local Humanoid = Character:FindFirstChild("Humanoid")
06    if Humanoid then
07         local currentHealth = Humanoid.Health
08         local newHealth = currentHealth + damageAmount
09         Humanoid.Health = newHealth       
10       end
11end)

I used a simple local script to change the Parent:

1local Tool = script.Parent
2Tool.Activated:Connect(function()
3local Example = script.Parent.Part
4Example.Parent = game.Workspace
5Tool:Destroy()
6end)

Here is how everything is arranged:

1Tool (Tool)
2    LocalScript (The parent - changer)
3    Part (Part that has damage script applied to it)
4          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)

1local event = Instance.new('RemoteEvent',script.Parent)
2event.Name = "ChangeParentEvent" -- create a remote event
3 
4event.OnServerEvent:Connect(function(plr,object,parent) -- Event for when the remoteevent is fired
5    object.Parent = parent -- Set the part's parent to workspace
6    object.Parent:Destroy() -- Destroy tool
7end)

Server Script (Damage Script inside Part)

01local Damagepack = script.Parent
02local damageAmount = -25                 
03Damagepack.Touched:Connect(function(otherPart)
04    local Character = otherPart.Parent
05    local Humanoid = Character:FindFirstChild("Humanoid")
06    if Humanoid then
07       local currentHealth = Humanoid.Health
08       local newHealth = currentHealth + damageAmount
09       Humanoid.Health = newHealth       
10    end
11end)

Local Script (Script inside of tool)

1local Tool = script.Parent
2local event = Tool:WaitForChild('ChangeParentEvent')
3 
4Tool.Activated:Connect(function()
5    local Example = script.Parent.Handle
6    event:FireServer(Example,workspace)
7    Tool:Destroy()
8end)

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:

1local Damagepack = script.Parent       
2 
3Damagepack.Touched:Connect(function(otherPart)
4    if otherPart.Parent:FindFirstChild("Humanoid") then
5        hit.Parent.Humanoid:TakeDamage(25-- to change the damage just change the number inside the arguments    
6       end
7end)

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:

1local Damagepack = script.Parent    
2local damageAmount = 25  
3 
4Damagepack.Touched:Connect(function(otherPart)
5    if otherPart.Parent:FindFirstChild("Humanoid") then
6        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - damageAmount
7       end
8end)
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