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

Are you not able to return a value in a function to an event function call?

The gist of the program I've written is for a user to touch a part, and then create a variable called "owner" that is equal to their player ID. Right now, I'm just trying to print the owner/UserID. Except the output keeps displaying "Connection - Server - Script:17" before I even touch the part. Which is why I'm wondering, is the "playerId" variable not allowed to be returned to the event function call? And if so, how can I create a variable outside the scope of a function equal to a variable inside the scope of the function? (And also, if anybody knows, what "Connection - Server - Script:17" means?)

local Players = game:GetService("Players")
local touch2claim = script.Parent.touch2claim


local function claim(otherPart)
	local partParent = otherPart.Parent
	local humanoid = partParent:FindFirstChild("Humanoid")
	if humanoid then
		local character = humanoid.Parent
		local player = Players:GetPlayerFromCharacter(character)
		local playerId = player.UserId
		return playerId
	end
end

local owner = touch2claim.Touched:Connect(claim)
print(owner)

I'm fairly new to programming on Roblox. So any help is appreciated!

EDIT: I think I may have solved it. Here is my new code:

local Players = game:GetService("Players")
local touch2claim = script.Parent.touch2claim


local owner = nil


local otherPart = touch2claim.Touched:Wait()
local partParent = otherPart.Parent
local humanoid = partParent:FindFirstChild("Humanoid")
if humanoid then
	local character = humanoid.Parent
	local player = Players:GetPlayerFromCharacter(character)
	local playerId = player.UserId
	owner = playerId
end

print(owner)

Feedback is appreciated!

2 answers

✓ Accepted answer

The variable owner is set to the connection value of the event Touched. This is because connections have a value that can be assigned to a variable. When you assign a connection to a variable, you can disconnect the function which was previously connected to the event.

An example of disconnecting a connection would be:

local connection = game.Players.PlayerAdded:Connect(function() -- this is where the connection is created
	
	-- logic code

end)

--...

connection:disconnect() 
-- Now that the event is disconnected, when the event is fired the function that connected to the function will not run

The value returned in the claim function will not be passed to the owner value.

I would recommend doing something like this:

local Players = game:GetService("Players")
local touch2claim = script.Parent.touch2claim

local owner = nil -- initialization

local function claim(otherPart)
    local partParent = otherPart.Parent
    local humanoid = partParent:FindFirstChild("Humanoid")
    if humanoid then
        local character = humanoid.Parent
        local player = Players:GetPlayerFromCharacter(character)
        local playerId = player.UserId
        owner = playerId
    end
end

touch2claim.Touched:Connect(claim)
print(owner)

If you have more questions, or this confused you at all, feel free to contact me on Discord (phxntxsmic#2021)

EDIT

I forgot to mention why you are getting the output that you are getting.

That is the connection which is being outputted as a string. The reason that it is outputting before you touch a part is because it is not really 'linked' to the connection, rather it is the connection. Due to it being the connection, it does not fire when the event is fired, but rather after the connection is created.

4 comments

0

the way you're explaining is kinda confusing to me.

0

If there is a way I can clear it up, I will gladly do so.

0

i mean i understand, but beginners might don't understand

0

Fair enough, although it isn't an easy topic to understand either, especially for beginners.

#try

local Players = game:GetService("Players")
local touch2claim = script.Parent.touch2claim


touch2claim.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		return print(player.UserId)
	end
end)

if it doesn't work dm me on #Blue Duck#8344

5 comments

0

https://developer.roblox.com have everything you need

0

When I tested your method, I got an error: "Attempt to connect failed: Passed value is not a function".

0

let me fix

0

edited

0

edited

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