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

For my simple snowball script, I don't understand why its returning this in output?

Its been a while since i've coded... So I have these two scripts: They are both local scripts.

--THROW SNOWBALL SCRIPT
local debounce = true 
local tool = script.Parent.Parent
local snowball = script.Parent

local Player = game.Players.LocalPlayer
local Speed = 75
local Height = 50

tool.Activated:Connect(function()
	if not debounce then
		return
	end
	debounce = false
	--Do a throw--
	local snowballclone = snowball:Clone()
	snowballclone.Parent = workspace
	snowballclone.Name = "ThrownSnowball"
	snowballclone.CanCollide = true
	snowballclone.Velocity = snowball.CFrame.LookVector*Speed + Vector3.new(0,Height,0)
	wait(2)
	debounce = true
end)

And this is my kill with snowball script

--KILL WHEN HIT
script.Parent.Touched:Connect(function(plr)
	print(hit) -- this prints as the actual Cloned snowball as if it's hitting itself and not anything else Im comfoosed?
	if plr.Parent.Humanoid then -- this obviously wont work if the hit is the snowball itself 
		plr.Parent.Humanoid.Health = plr.Parent.Humanoid.Health - 30
	end
end)

In the output it says: ThrownSnowball

So how do I actually make it print the player instead of the snowball itself?

1 comment

0

Sir I don't think you can kill another player with a local script >.>

1 answer

GENERAL PRACTICE

In the first LocalScript, change game.Players to game:GetService("Players") as this is a more reliable way of retrieving the Players Service

In the second LocalScript, use :FindFirstChild() to check that an instance is true rather than using hit.Parent.Humanoid (as this would return nil)

ISSUES

The .Touched event parameter is the part that touched the connect part, so if a part named "Part2" touched the snowball then in the below code "hit" would be "Part2"

When a player touches an object, the "hit" is the part that touched the "script.Parent" where it is presumed the parent is the Character model of the player (however we check that the humanoid exists since items like accessories have handles who are parented under another object below the model).

In order to print the player's name, you can simply print the character model name as seen below:

REVISED LOCAL SCRIPT (2)

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		print(hit.Parent.Name)
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 30
	end
end)

However, if you wanted to grab the player's instance, you can use the :GetPlayerFromCharacter() function of the Players Service, where the parameter is the character model. An example of this can be seen below.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player ~= nil then
			print(player.Name)
			hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - 30
		end
	end
end)

ADDITIONAL FEEDBACK

I will also note that since you are using a LocalScript, any players who are hit by the snowball will only die on that player's screen. Instead, you will require a remote event to clone the snowball on the server and change the localscript used to deal damage into a serverscript in order for all damage to be seen by all players (I will gladly take comments or questions regarding this method as well)

Link about Remote Events / Functions: https://developer.roblox.com/api-reference/class/RemoteEvent

1 comment

0

Well, this could also be done on the server to be FE compatible and prevent the unnecessary hassle of using remotes

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