Hey there. I’m trying to get the explosion to be switched to the player if they have the bomb, not the person who originally had it (unless they didnt pass the bomb of course)
I’m really not trying to take code. I want to learn Luau. I enjoy learning coding. But this is one of the times where I’m confused.
As always, sorry if the code looks crappy, I’m an intermediate.
Here are the 2 scripts that will probably most likely need updates.
local tool = script.Parent
local plrs = game.Players
local debounce = false
local explosion = Instance.new("Explosion")
local player = script:FindFirstAncestorWhichIsA("Player") or game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local hitbox = char:WaitForChild("Hitbox")
if not debounce then
debounce = true
wait(15)
explosion.Parent = workspace
explosion.Position = char.HumanoidRootPart.Position
tool:Destroy()
end
local plrs = game.Players
local debounce = false
plrs.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local hitbox = game.ServerStorage:WaitForChild("Hitbox"):Clone()
hitbox.Parent = char
hitbox.CanCollide = false
hitbox.Position = char.HumanoidRootPart.Position
local weld = Instance.new("WeldConstraint")
weld.Parent = hitbox
weld.Name = ("HitBoxWeld")
weld.Part0 = hitbox
weld.Part1 = player.Character.HumanoidRootPart
hitbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
local plr = plrs:GetPlayerFromCharacter(hit.Parent)
local bomb = player.Character:WaitForChild("Bomb")
if not debounce then
debounce = true
bomb.Parent = plr.Backpack
wait(2)
debounce = false
end
end
end)
end)
end)
Nothing came out of the output. The script works, I just want it to make it so that the player who has the bomb when the timer runs out gets exploded, not the person who originally had it.
Try looping through all the players, checking if the have the tool, and then exploding that player. I can give you more details / hints if you need em.
Still confused. I added a loop but it still explodes the person who originally had it. Heres the updated code. local tool = script.Parent local plrs = game.Players local debounce = false local explosion = Instance.new("Explosion") local player = script:FindFirstAncestorWhichIsA("Player") or plrs:GetPlayerFromCharacter(script.Parent.Parent) local char = player.Character or player.CharacterAdded:Wait() local human = char:WaitForChild("Humanoid") while wait() do local bomb = player.Backpack:FindFirstChild("Bomb") wait(15) if bomb then explosion.Parent = workspace explosion.Position = char:WaitForChild("HumanoidRootPart").Position end end
Im guessing the tool isn’t recreated when the bomb is passed. If so, you would have to change the variables in the bomb each time it is passed around. For example, you could use the .AncestryChanged event in the bomb tool to detect whenever the bomb is passed around. From there, you can change the variables (player & character) as you see fit.
This is just a demo, but is this correct? local bomb = script.Parent bomb.AncestryChanged:Connect(function() local parent = bomb.Parent if parent then print("parent changed to " ..parent.Name) end end)
Edit: Just tested it, and it doesn’t work sadly. The video is weirdly cropped, but you can see the bomb dropping from my hands. What I did is just reparent it to the workspace.
This would only detect when the parent changes, and I don’t think that’s what you were going for.
Here are the changes I made to your first script.
local tool = script.Parent
local plrs = game.Players
local debounce = false
local explosion = Instance.new("Explosion")
local player = script:FindFirstAncestorWhichIsA("Player") or game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local hitbox = char:WaitForChild("Hitbox")
if not debounce then
debounce = true
wait(15)
explosion.Parent = workspace
explosion.Position = char.HumanoidRootPart.Position
tool:Destroy()
end
tool.AncestryChanged:Connect(function() --Here we are checking if the parent or parent's parent has changed.
if tool.Parent == nil then return end --This is checking if the tool has a parent. If it doesnt, we will stop this block of code.
local humanoid = tool.Parent:FindFirstChild("Humanoid") --Here we are checking to see if the tool's Parent has a humanoid
if humanoid and game.Players:GetPlayerFromCharacter(tool.Parent) then --Here we are checking to see if the humanoid exists and if the tool's parent is a character.
player = game.Players:GetPlayerFromCharacter(tool.Parent) --Here we are changing the variables, so that it isn't only the first person that gets nuked.
char = player.Character --Read above comment
end
end)
I don’t know what you need the other variables for, so I left them.
Hope this helps!
Haha finally got it to work. I really hate being spoonfed and I want to learn, so I’ll look at uses for AncestryChanged sometime. There was one small problem though. The ancestry changed event was swapped out with the wait event. When I swapped the places of those 2, it worked. Here is the updated code
local tool = script.Parent
local plrs = game.Players
local debounce = false
local explosion = Instance.new("Explosion")
local player = script:FindFirstAncestorWhichIsA("Player") or game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local hitbox = char:WaitForChild("Hitbox")
tool.AncestryChanged:Connect(function()
if tool.Parent == game.ServerStorage or nil then return end
local humanoid = tool.Parent:FindFirstChild("Humanoid")
if humanoid and game.Players:GetPlayerFromCharacter(tool.Parent) then
player = game.Players:GetPlayerFromCharacter(tool.Parent)
char = player.Character
end
end)
if not debounce then
debounce = true
wait(15)
explosion.Parent = workspace
explosion.Position = char.HumanoidRootPart.Position
tool:Destroy()
end