Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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.

0
the way you're explaining is kinda confusing to me. WINDOWS10XPRO 438 — 3y
0
If there is a way I can clear it up, I will gladly do so. appxritixn 2235 — 3y
0
i mean i understand, but beginners might don't understand WINDOWS10XPRO 438 — 3y
0
Fair enough, although it isn't an easy topic to understand either, especially for beginners. appxritixn 2235 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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

0
https://developer.roblox.com have everything you need WINDOWS10XPRO 438 — 3y
0
When I tested your method, I got an error: "Attempt to connect failed: Passed value is not a function". appxritixn 2235 — 3y
0
let me fix WINDOWS10XPRO 438 — 3y
0
edited WINDOWS10XPRO 438 — 3y
0
edited WINDOWS10XPRO 438 — 3y

Answer this question