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

Help With Targeting Players?

script.Parent.MouseButton1Click:connect(function(lool)
	local name = script.Parent.Parent.Name.Text
	for i,v in pairs(game.Players:GetChildren()) do
		if v.Name == name then
			for i,z in pairs(game.Workspace.Zombies:GetChildren()) do
				if z.Owner.Value == script.Parent.Parent.Parent.Parent.Parent.Parent.Name then
					z:MoveTo(v.Torso)
				end
			end
		end
	end
end)

This script is basically supposed to get a zombie to attack the player that has their name written in a box. It doesn't work. No errors or anything. Help please?

2 comments

0

What is the hierarchy of your gui?

1 answer

From my knowledge, normal scripts will not work inside PlayerGui. If you want a player to have control via GUI over something all players see (a server-side thing like your zombies) you'll have to use Remote Events and a Local Script. I'll give you my rendition of this, but promise me you'll learn from it.

This will be a normal script located in ServerScriptService (You can put it anywhere not related to a player, but I would put it in there):

local ZAttack = Instance.new("RemoteEvent", game.ReplicatedStorage)
ZAttack.Name = "ZAttack"
ZAttack.OnServerEvent:connect(function(player,name) --'player' is the client that calls the function, 'name' is what that player inputs into the function's argument(or was it called parameter? idk I don't have formal training.)
	local target = game.Players:FindFirstChild(name)
	if target == nil then return end --Switched out the check if the target exists
	for i,z in pairs(game.Workspace.Zombies:GetChildren()) do
		if z.Owner.Value ~= player.Name then --Your original check would've left you vulnerable too!
			z:MoveTo(target.Character.Torso) --Players don't have torsos but their characters do!
		end
	end
end)

And put this in a Local Script that will replace your current one:

script.Parent.MouseButton1Click:connect(function()
	game.ReplicatedStorage.ZAttack:FireServer(script.Parent.Parent.Name.Text) --That's the Argument/Parameter in the parenthesis that I mentioned before!
end)

tl;dr I did it for you, dissect my script(s) so you can do it too!

1 comment

0

Tried all that, still doesn't work and doesn't give me error messages. Also, regular scripts do work in Gui, I've gotten other regular scripts in Guis that effect the zombies in other ways. I also figured out that a lot of what's wrong from the script was because I was tired when I was writing it, like when I tried to get the Zombie to go to a torso inside the player. Lol. I fixed up the script a

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