How can I get player?

I have a speed orb which it increases the player speed but it’s annoying when I have to use “player” to send the remote event. Is there a way I can get the player without using game.players?

local SpeedOrb = script.Parent

local player = game.Players

player.PlayerAdded:Connect(function(plr)
	SpeedOrb.Touched:Connect(function(hit)
	local hum = hit.Parent:FindFirstChild("Humanoid")
	
		if hum then	
		game.ReplicatedStorage.SpeedOrb:FireClient(plr)
		
	end
	
	end)
	
end)


2 Likes

Why did you delete that code? Does that method does not work?

You can get the player without using game.Players by directly using the Touched event. You can modify your script like this for example:

local SpeedOrb = script.Parent

SpeedOrb.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    
    if humanoid then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            game.ReplicatedStorage.SpeedOrb:FireClient(player)
        end
    end
end)

This way, you don’t need to use game.Players outside of the event and directly get the player associated with the character that touches the orb, that’s it.

1 Like

Ah, I see, thank you so much! I always wondered how to do it but I couldn’t find it anywhere else. Thanks man!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.