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

How would I use LocalPlayer in a server script?

This is what I have in the classic paintball gun, from line 29 in 'Paintball'

	if humanoid ~= nil or localplayer then
		tagHumanoid(humanoid)
		humanoid:TakeDamage(damage)
		wait(1)
		untagHumanoid(humanoid)
	end

The entire script:

ball = script.Parent
damage = 50

function onTouched(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if hit:getMass() < .5 * 200 then
		hit.BrickColor = ball.BrickColor
	end
for i=1, 1 do
	local s = Instance.new("Part")
	s.Shape = 1 
	s.formFactor = 2 
	s.Size = Vector3.new(1,.4,1)
	s.BrickColor = ball.BrickColor
	local v = Vector3.new(math.random9(-1,1), math.random(0,1), math.random(1,1))
	s.Velocity = 15 * v
	s.CFrame = CFrame.new(ball.Position + v, v)
	ball.BrickCleanup:clone().Parent = s
	s.BrickCleanup.Disabled = false
	s.Parent = game.Workspace	
end

if humanoid ~= nil then
	 tagHumanoid(humanoid)
		humanoid:TakeDamage(damage)
		wait(1)
		untagHumanoid(humanoid)
end

connection:Disconnect()
ball.Parent = nil
end

function tagHumanoid(humanoid)
	local tag = ball:FindFirstChild("creator")
	if tag ~= nil then
		local new_tag = tag.clone()
	end
end

function untagHumanoid(humanoid)
	if humanoid ~= nil then
		local tag = humanoid:FindFirstChild("humanoid")
		if tag ~= nil then
			tag.Parent = nil
		end
	end
end

connection = ball.Touched:connect(onTouched)
wait(3)
ball.Parent = nil

2 answers

The server has no 'Local Player'. It's only achievable inside a local script because local scripts run on the clients' computer, rather than a server script, which only runs on the server and has no relations with any of the clients therefore local player doesn't exist on the server.

However, there are other ways of getting the player on the server, if you're using events such as a Touched event which returns the part that came into contact with the given part, you can then find the player by using the Players:GetPlayerFromCharacter() function and include the parent of the part that was returned. If it was a character's body part that was returned, then the parent of that part would be the character.

5 comments

0

I need the script to check if the player that touched the paintball is the player that shot it so they won't get damaged. I added the entire script to the question so you can see what I need to add.

0

Okay I see that the script you provided is inside the paintball, right? So, what you could do is add an attribute inside the ball and set the name to "playerThatShot" and set the value to the player's name. If you've never used attributes, they're pretty much like custom properties that you can add to an object.

0

So, navigate to wherever you are shooting the ball (assuming its a script inside the tool itself, you can get the player's character by using script.Parents) and set the attribute value to the character's name. And now you can get this attribute to check to see if the characters' name that was hit by the ball is the same as the player that shot it.

0

^ Hopefully its not too confusing. If you're having trouble with Attributes there are tutorials on youtube that shows you how they work (or google Attributes for roblox (should be the first link) and on the page it'll tell you how they work and you'll also be able to find all the functions and events for attributes).

0

When I use SetAttribute, I get the error "Instance is not a supported attribute type" when I shoot the gun, and the ball doesn't get created.

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