Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why are the npcs not taking damage when the part is cloned into workspace?

I need the part to damage the npcs when it is summoned but the script doesn't do anything. But when the part is in the workspace by default, it works can u pls help me SUMMONING CODE

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(input, event)
    if input.KeyCode == Enum.KeyCode.Q then
	local Justice = game:GetService("ReplicatedStorage").kills
	wait(.0001)
	local justice = Justice:Clone()
    justice.Parent = game.Workspace
	justice.Position = script.Parent.Parent.LowerTorso.Position - Vector3.new(0,2,0)
	justice.CanCollide = false
	justice.Anchored = true
	script.Parent.Parent.Humanoid.WalkSpeed = 0
	wait(.0001)
	script.Parent.Parent.Humanoid.MaxHealth = 1000
	script.Parent.Parent.Humanoid.Health = 1000
	script.Parent.Parent.LowerTorso.CFrame = script.Parent.Parent.LowerTorso.CFrame + Vector3.new(0,10,0)
	script.Parent.Parent.LowerTorso.Anchored = true
	wait(4)
	justice.jp.Enabled = true
	justice.dmg.Disabled = false
	justice.Transparency = 0
	wait(0.5)
	script.Parent.Parent.Humanoid.MaxHealth = 100
	script.Parent.Parent.Humanoid.Health = 100
	justice:Destroy()
	script.Parent.Parent.LowerTorso.Anchored = false
	script.Parent.Parent.Humanoid.WalkSpeed = 16
end
end)

DAMAGE CODE (INSIDE THE PART)

function onTouched(hit)
        local human = hit.Parent:findFirstChildOfClass("Humanoid")
        if (human ~= nil) then
                human.Health = human.Health - 42000 -- Change the amount to change the damage.
        end
end
script.Parent.Touched:connect(onTouched)

3 comments

0

Is your Summoning Code in a server script or LocalScript?

0

summoning is local

0

its local so that only one person gets it and not everybody because you have to equip the tool

1 answer

✓ Accepted answer

Now to understand the problem, you first need to understand the FilteringEnabled feature that roblox rolled out I belive in 2017 or 2018.. LocalScripts run on the player's device, but server scripts run on the server that connects all the players. LocalScripts are meant to do changes that only a affect a single player - like manipulating the player's camera, or moving GUIs around.. On the other hand, server scripts are meant to do things that all the players can see - because remember, all players are connected to the server.. Before FilteringEnabled was added to roblox, a developer could use either LocalScripts or server scripts to do things that replicated to all players, but this backfired because exploiters used this vulnerability to do and show really disturbing things in roblox, so FE was rolled out.. FilteringEnabled keeps changes that happen in LocalScripts from being replicated to the server..

so how does correspond to your problem? the thing is your summoning the kill brick using a LocalScript, therefore, the server doesn't actually know when you have "summoned" the brick, therefore, nothing happens.. but when the brick is originally in workspace, the script in it is automatically ran.. That's why it kills when initially in workspace, but not when after being cloned..

Now to allow developers to have a localscript communicate with the server, roblox added RemoteEvents and RemoteFunctions..

so here is how you could do it..

--server script

local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Players = game:GetService("Players");

local KillBrick = ReplicatedStorage.kills

local RemoteEvent = Instance.new("RemoteEvent", ReplicatedStorage);
RemoteEvent.Name = "MakeKillBrick";


RemoteEvent.OnServerEvent:Connect(function(player)
	local killbrick = KillBrick:Clone();	
	killbrick.Parent = workspace
end)


Players.PlayerAdded:Connect(function(player)
	local localscript = script:FindFirstChildWhichIsA("LocalScript"):Clone();
	localscript.Parent = player.PlayerGui
	localscript.Disabled = false
end)

--this is a localscript, put it inside the server script above, and disable it..

local Players = game:GetService("Players");
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local UserInputService = game:GetService("UserInputService");

local RemoteEvent = ReplicatedStorage:WaitForChild("MakeKillBrick");

local player = Players.LocalPlayer
script.Parent = player:WaitForChild("PlayerScripts");


UserInputService.InputBegan:Connect(function(input)
	if(input.KeyCode == enum.KeyCode.Q) then
		RemoteEvent:FireServer();
	end
end)

more resources to help you out..

AlvinBlox;

DevHub

good luck..

This discussion is preserved as it was. New answers, comments, and votes are closed.